使用 python 从文件中读取浮点数并仅存储小数
Using python read floats from a file and store only the decimals
所以我在操作系统 class 中,我的任务是从盖革计数器中获取位。每次盖革计数器报告我收到一个时间戳,即 1522187398.44 1522187408.17 每行有一个时间戳。我目前有 22,000 行数字。从这里开始,我将一次获取 16 行,并使用这些行创建 8 位,然后将其转换为 ASCII 字符。由于我的时间戳不断增加,我意识到小数点随机高于或低于前一个。目前我正试图弄清楚如何只保留小数并将它们存储到列表中。我确实探索了一些关于 modf 和从文件中读取的问题,但我不断收到语法错误,告诉我在第 11 行有一个类型错误:需要一个浮点数。教授要求我使用 python 2.7(我们在作业的后半部分使用 FUSE 文件系统,但这与这个问题无关)。如果有人可以在这部分帮助我,我相信我可以完成任务。我目前的代码在下面。任何帮助将不胜感激。
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])
使用您的浮点数列表,您可以执行以下操作。将所有值转换为字符串
numbers = [10.9854, 8.284729, 7.1248, 8.23784]
numbers = [str(i) for i in numbers]
然后找到小数点,提取小数点后的所有数字
print([i[i.find(".")+1:len(i)] for i in numbers])
['9854', '284729', '1248', '23784']
# 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])
所以我在操作系统 class 中,我的任务是从盖革计数器中获取位。每次盖革计数器报告我收到一个时间戳,即 1522187398.44 1522187408.17 每行有一个时间戳。我目前有 22,000 行数字。从这里开始,我将一次获取 16 行,并使用这些行创建 8 位,然后将其转换为 ASCII 字符。由于我的时间戳不断增加,我意识到小数点随机高于或低于前一个。目前我正试图弄清楚如何只保留小数并将它们存储到列表中。我确实探索了一些关于 modf 和从文件中读取的问题,但我不断收到语法错误,告诉我在第 11 行有一个类型错误:需要一个浮点数。教授要求我使用 python 2.7(我们在作业的后半部分使用 FUSE 文件系统,但这与这个问题无关)。如果有人可以在这部分帮助我,我相信我可以完成任务。我目前的代码在下面。任何帮助将不胜感激。
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])
使用您的浮点数列表,您可以执行以下操作。将所有值转换为字符串
numbers = [10.9854, 8.284729, 7.1248, 8.23784]
numbers = [str(i) for i in numbers]
然后找到小数点,提取小数点后的所有数字
print([i[i.find(".")+1:len(i)] for i in numbers])
['9854', '284729', '1248', '23784']
# 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])