简体   繁体   English

如何将文本文件中的一维数据读入一维数组? [Python]

[英]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.

相关问题 如何从文本文件中的列中读取数据到 python 中的 3 1D numpy arrays - How can you read data from columns in a text file into 3 1D numpy arrays in python 如何从一维源填充HDF5中的n维数组? - How do I fill an n-dimensional array in HDF5 from a 1D source? 如何将1D数据转换为2D数组? - How do I convert 1D data into a 2D array? 如何从python将一维数组的数据写入Excel文件? - How to write data from 1D array to an excel file from python? 如何在 Python 中将文本文件中的行读入二维数组 - HOW TO READ LINE FROM A TEXT FILE INTO AN 2D ARRAY IN PYTHON Matlab-> Python,如何将二进制文件转换为一维浮点数组? 在Matlab中工作正常,但无法在Python中重现 - Matlab -> Python, how do I convert binary file to 1d float array? Works fine in Matlab but unable to reproduce in Python 我如何在Python Numpy中将2D数组和1D数组相减并相除? - How do I subtract and divide a 2D array and a 1D array in Python Numpy? python将2d读入1d数组 - python read 2d in to a 1d array 在python中读取一维文本文件并将其排序到多维数组 - Reading and sorting a 1D text file to a multidimensional array in python 如何将2D numpy数组转换为1D numpy数组的1D numpy数组? - How do I convert a 2D numpy array into a 1D numpy array of 1D numpy arrays?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM