[英]How do I read 1 dimensional data from a text file into a 1D array? [Python]
I have a text file DATALOG1.TXT with values from 0 to 1023, pertaining to a signal.我有一个文本文件 DATALOG1.TXT,其值从 0 到 1023,与信号有关。 The file has one value per row, all values stored in a column.
该文件每行有一个值,所有值都存储在一列中。 Here is an example of how the first values are stored in the file:
这是第一个值如何存储在文件中的示例:
0
0
576
0
643
60
0
1012
0
455
69
0
1023
0
258
I have the following code, which outputs for the "magnitude" only values between 0 to 9, as if normalized.我有以下代码,它只输出 0 到 9 之间的“幅度”值,就像标准化一样。 Here's my code:
这是我的代码:
import matplotlib.pyplot as plt
import numpy as np
values = [] #array to be filled
#reading data from the file into the array
f = open('DATALOG1.TXT', 'r')
for line in f:
values.append(line[0])
#creating another array of the same size to plot against
time = np.arange(0, len(values), 1)
plt.bar(time, values, color='g', label='File Data')
plt.xlabel('Time', fontsize=12)
plt.ylabel('Magnitude', fontdsize=12)
plt.title('Magnitude changes', fontsize=20)
plt.show()
Why is the output between 0 and 9 as opposed to showing the values in the text file (between 0 and 1023)?为什么 output 介于 0 和 9 之间,而不是显示文本文件中的值(介于 0 和 1023 之间)?
Your problem lies here:你的问题出在这里:
for line in f:
values.append(line[0])
What you append is the first character of each line to your list values
.你 append 是每行的第一个字符到你的列表
values
。 The list values
contains the entire line including line endings.列表
values
包含整行,包括行尾。 If you were to use如果你要使用
for line in f:
values.append(line.strip())
you should be a lot closer to your final answer.你应该更接近你的最终答案。
By taking line[0]
you just append the first character of each line to the list.通过使用
line[0]
,您只需 append 将每行的第一个字符添加到列表中。
Since you already use numpy, you can use genfromtxt
to load the data file.由于您已经使用了 numpy,因此您可以使用
genfromtxt
加载数据文件。
Your code would become:您的代码将变为:
import matplotlib.pyplot as plt
import numpy as np
values = [] #array to be filled
#reading data from the file into the array
values = np.genfromtxt('DATALOG1.TXT')
#creating another array of the same size to plot against
time = np.arange(0, len(values), 1)
plt.bar(time, values, color='g', label='File Data')
plt.xlabel('Time', fontsize=12)
plt.ylabel('Magnitude', fontsize=12)
plt.title('Magnitude changes', fontsize=20)
plt.show()
I suggest to read the complete file, split the values per newline character \n
(i used list comprehension for that) and to convert strings to integers.我建议阅读完整的文件,拆分每个换行符
\n
的值(我为此使用了列表理解)并将字符串转换为整数。 Also, if you use range
instead of numpys arange
you can avoid using numpy at all.此外,如果您使用
range
而不是 numpys arange
,则可以完全避免使用 numpy。
import matplotlib.pyplot as plt
#reading data from the file into the array
data = open('DATALOG1.TXT', 'r').read()
values = [int(f) for f in data.split("\n")]
time = range(len(values))
plt.bar(time, values, color='g', label='File Data')
plt.xlabel('Time', fontsize=12)
plt.ylabel('Magnitude', fontdsize=12)
plt.title('Magnitude changes', fontsize=20)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.