简体   繁体   English

Python:从 TXT 文件导入逗号分隔的数据

[英]Python: Import comma separated data from a TXT file

I built a data logger with a Raspberry Pi Pico.我用 Raspberry Pi Pico 构建了一个数据记录器。 The Data Logger saves the temperature every six minutes in the TXT file.数据记录器每六分钟将温度保存在 TXT 文件中。 I am currently failing to import the data from the TXT file into Python.我目前无法将 TXT 文件中的数据导入 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,...数据如下所示: 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.该数据集有大约 240 个测量值。

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:我已将 TXT 文件转换为 CSV 文件,并尝试使用 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.这给了我一个 df 一个观察值和一个三位数的变量。 I cannot import the dataset to have only two variables and about 240 observations.我无法将数据集导入只有两个变量和大约 240 个观察值。

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".这导致错误: "TypeError: 'list' object is not callable".

--> All in all, I need a solution that uses the TXT file directly and creates a df. --> 总而言之,我需要一个直接使用TXT文件并创建一个df的解决方案。 I am still very inexperienced with Python and hope for your help!我对 Python 还是很陌生,希望能得到您的帮助! Thanks in advance提前致谢

You should try pd.read_fwf.你应该试试 pd.read_fwf。 It is for reading fixed width format.它用于读取固定宽度格式。

https://pandas.pydata.org/docs/reference/api/pandas.read_fwf.html https://pandas.pydata.org/docs/reference/api/pandas.read_fwf.html

I would use numpy here.我会在这里使用 numpy。 Numpy are python lists with nice functionalities. Numpy 是具有很好功能的 python 列表。 They allow you to save/retrieve the list in a .npy file.它们允许您在 .npy 文件中保存/检索列表。

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.但是,“.npy”文件不是人类可读的。

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

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()})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM