-1

So I am in a operating system class and my assignment is to harvest bits from a geiger counter. Every time the geiger counter reports something I receive a timestamp i.e. 1522187398.44 1522187408.17 there is one timestamp per line. I currently have 22,000 lines of numbers. From here I am to take 16 lines at a time and use those lines to create 8-bits that I will then convert into an ASCII character. Since my time stamps keep increasing I realized that the decimal points are randomly higher or lower then the previous. Currently I am trying to figure out how to just keep the decimals and store them into a list. I did explore a some asked questions about modf and reading from a file but I keep getting an syntax error telling me that at line 11 there is a TypeError: a float is required. I am required to use python 2.7 by the professor (We are using FUSE file system for the second half of the assignment but that is irrelevant for this question) . If someone could please help me at this part I am confident I can finish the assignment. My code as it is currently is down below. Any help would be appreciated.

import math
numbers = []

#Open file with timestamps and store in a list
with open('geiger.txt') as f:
    for line in f:
        numbers.append(map(float, line.split()))

#Keep only the decimals and move decimal place
for convert in numbers:
    numbers = math.modf(convert) * 100

#Check to see if it worked
print(numbers[0:11])
GenCrash10
  • 9
  • 1
  • 5
  • 2
    When you say you want to keep the "decimals", do you mean the numbers before the decimal, or after? And why do you want only one of them? This seems a bit like an [XY problem](http://xyproblem.info/) – Brendan Abel Apr 09 '18 at 04:03

2 Answers2

0

With your list of floating point numbers you can do the following. Convert all the values to strings

numbers = [10.9854, 8.284729, 7.1248, 8.23784]
numbers = [str(i) for i in numbers]

Then find the decimal point, and extract all numbers after it

print([i[i.find(".")+1:len(i)] for i in numbers])

['9854', '284729', '1248', '23784']

JahKnows
  • 2,618
  • 3
  • 22
  • 37
0
# import math
# numbers = []

#Open file with timestamps and store in a list
with open('geiger.txt') as f:
    # list comprehension is a a more pythonic way to do this
    # if there is only one timestamp per line there is no point in using split()
    numbers = [line.replace('\n', '') for line in f]
    # for line in f:
    #     numbers.append(map(float, line.split()))

#Keep only the decimals and move decimal place

# since you only need the decimal numbers use split('.') to get them
# for this to work the number type is a string not float
decimal_numbers = [convert.split('.')[1] for n, convert in enumerate(numbers)]

#Check to see if it worked
print(numbers[0:11])
print(decimal_numbers[0:11])
avaj_wen12
  • 213
  • 1
  • 4