Python:导入逗号与TXT文件分开的数据

发布于 2025-02-12 16:28:31 字数 735 浏览 1 评论 0原文

我用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光与爱终年不遇 2025-02-19 16:28:31

这有帮助吗?

import pandas


data = dict()

with open("data.txt") as f:
    for line in f.readlines():
        splitted = line.split(",")
        data = {**data, **dict(zip(splitted[::2], splitted[1::2]))}

as_dataframe = pandas.DataFrame({"counter": data.keys(), "temperature": data.values()})

Does this help?

import pandas


data = dict()

with open("data.txt") as f:
    for line in f.readlines():
        splitted = line.split(",")
        data = {**data, **dict(zip(splitted[::2], splitted[1::2]))}

as_dataframe = pandas.DataFrame({"counter": data.keys(), "temperature": data.values()})

情绪操控生活 2025-02-19 16:28:31

您应该尝试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

两人的回忆 2025-02-19 16:28:31

我会在这里使用numpy。 Numpy是具有良好功能的Python列表。它们允许您将列表保存/检索.npy文件。

例如。

import numpy as np
import pandas as pd

data = [1,20.5,2,21.0,3,21.0,4,21.0,5,21.0,6,21.0,7,21.0]
data_numpied = np.array(data,dtype=float)
np.save("numpy_data_storage.npy",data_numpied,allow_pickle = True)

# Restart / exit here

new_data = [42,42,1.0]
old_data = np.load("numpy_data_storage.npy",allow_pickle=True)
new_data2 = np.concatenate((old_data,new_data))

dataframe = pd.DataFrame(new_data2,columns = ["Temperature"])

但是,“ .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.

import numpy as np
import pandas as pd

data = [1,20.5,2,21.0,3,21.0,4,21.0,5,21.0,6,21.0,7,21.0]
data_numpied = np.array(data,dtype=float)
np.save("numpy_data_storage.npy",data_numpied,allow_pickle = True)

# Restart / exit here

new_data = [42,42,1.0]
old_data = np.load("numpy_data_storage.npy",allow_pickle=True)
new_data2 = np.concatenate((old_data,new_data))

dataframe = pd.DataFrame(new_data2,columns = ["Temperature"])

However, the ".npy" file is not human-readable.

远山浅 2025-02-19 16:28:31

给定输入文件

1,20.5,2,21.0,3,21.0,4,21.0,5,21.0,6,21.0,7,21.0`

作为例如,尝试以下操作:

import pandas as pd
df = pd.read_csv('temp_data.csv', header=None)
temp_df = pd.DataFrame(index=df.iloc[0,0::2].astype(int).values, data=dict(temp=df.iloc[0,1::2].values))
print(temp_df)

结果:

     temp
1  20.5
2  21.0
3  21.0
4  21.0
5  21.0
6  21.0
7  21.0

Given the input file

1,20.5,2,21.0,3,21.0,4,21.0,5,21.0,6,21.0,7,21.0`

as example, try this:

import pandas as pd
df = pd.read_csv('temp_data.csv', header=None)
temp_df = pd.DataFrame(index=df.iloc[0,0::2].astype(int).values, data=dict(temp=df.iloc[0,1::2].values))
print(temp_df)

Result:

     temp
1  20.5
2  21.0
3  21.0
4  21.0
5  21.0
6  21.0
7  21.0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文