尝试以.txt
格式csv
格式的pandas.read_csv()
文件读取测量数据时遇到问题。我的代码工作正常,但是当我尝试在for循环中组装文件路径时它失败并出现错误:
import os
import pandas as pd
root_folder = 'C:\\some\\root\\folder'
filename = 'Log_data.txt'
def get_data(root_folder, filename):
df_list = []
for folder in os.listdir(root_folder + '\\' + 'measurements'):
df = pd.read_csv(root_folder + '\\measurements\\' + folder + '\\' +
filename,
sep=' ',
names= ['Date','Time','Rx power','Rx snr',
'BNO Az','HMC Az','Ref Az','Now Az',
'CONTR TGT Az','YUN TGT Az',
'Ref TGT Az','TGT Az','BNO El',
'HMC El','Ref El','Now El',
'CONTR TGT El','YUN TGT El',
'Ref TGT El','TGT El','Ref Lat',
'YUN Lat','Now Lat','Ref Lon',
'YUN Lon','Now Lon','YUN Flag Az',
'YUN Flag El','YUN Modus',
'Ref HDG dGPS','Ref HDG Trailer'])
df['File'] = folder[9:]
df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d')
df['Time'] = pd.to_datetime(df['Time'], format='%H:%M:%S.%f')
df['Time'] = df['Time'] - df['Time'][0]
df['Time'] = df['Time'].dt.total_seconds()
df = df.set_index([df.index, 'File', 'Date', 'Time'], drop = True)
df_list.append(df)
df_big = pd.concat(df_list)
return df_big
df = get_data(root_folder, filename)
答案 0 :(得分:0)
我找到了错误源/解决方案。问题出在我的文件夹名称中的德语变音符号。我必须确保对read_csv()使用正确的编码。
df = pd.read_csv(root_folder + '\\Messdaten\\' + folder + '\\' + filename,
encoding='iso-8859-1', ....