Python:导入逗号与TXT文件分开的数据
我用Raspberry Pi Pico构建了数据记录仪。数据记录器每六分钟在TXT文件中节省每六分钟的温度。 我目前未能将数据从TXT文件导入到Python中。
数据看起来像这样: 1,20.5,2,21.0,3,21.0,4,4,21.0,5,5,21.0,6,6,21.0,7,21.0,...
数据集因此包含两个变量,这些变量由逗号,一个增加的计数器和一个小数位的温度。该数据集大约有240个测量值。
到目前为止,我已经尝试了不同的方法来导入数据。我已经将TXT文件变成了CSV文件,并尝试使用pandas作为数据帧进行导入:
temp_df = pd.read_csv('temp_data.csv',header = none
)以及三位数的变量。我不能将数据集导入只有两个变量和大约240个观测值。
我还试图将数据导入列表:
import csv
file = open("temp_data.csv", "r")
temp_list = list(csv.reader(file, delimiter=","))
file.close()
print(temp_list)
这导致错误: “ typeError:'list'对象不可callable”。
- >总而言之,我需要一个直接使用TXT文件并创建DF的解决方案。 我仍然对Python非常没有经验,并希望您的帮助! 提前致谢
I built a data logger with a Raspberry Pi Pico. The Data Logger saves the temperature every six minutes in the TXT file.
I am currently failing to import the data from the TXT file into Python.
The data looks like this:1,20.5,2,21.0,3,21.0,4,21.0,5,21.0,6,21.0,7,21.0,...
The data set thus contains two variables, which are separated by commas, an incrementing counter and the temperature with one decimal place. The dataset has about 240 measurements.
So far I have tried different methods to import the data. I have turned the TXT file into a CSV file and tried importing as a dataframe using pandas:
temp_df = pd.read_csv('temp_data.csv', header=None)
This gives me a df with one observation and a three-digit number of variables. I cannot import the dataset to have only two variables and about 240 observations.
I also tried to import the data as lists:
import csv
file = open("temp_data.csv", "r")
temp_list = list(csv.reader(file, delimiter=","))
file.close()
print(temp_list)
Which results in the error:"TypeError: 'list' object is not callable".
--> All in all, I need a solution that uses the TXT file directly and creates a df.
I am still very inexperienced with Python and hope for your help!
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这有帮助吗?
Does this help?
您应该尝试pd.read_fwf。它用于读取固定宽度格式。
https://pandas.pydata.org/docs/reference/api/ pandas.read_fwf.html
You should try pd.read_fwf. It is for reading fixed width format.
https://pandas.pydata.org/docs/reference/api/pandas.read_fwf.html
我会在这里使用numpy。 Numpy是具有良好功能的Python列表。它们允许您将列表保存/检索.npy文件。
例如。
但是,“ .npy”文件不可读。
I would use numpy here. Numpy are python lists with nice functionalities. They allow you to save/retrieve the list in a .npy file.
eg.
However, the ".npy" file is not human-readable.
给定输入文件
作为例如,尝试以下操作:
结果:
Given the input file
as example, try this:
Result: