import pandas as pd
from pathlib import Path
def backwardtrace(file: str):
if not Path(file).is_file():
raise ValueError('File does not exist!')
else:
res = []
with open(file) as f:
line = f.readline()
if line[0:4] != 'time':
raise ValueError('File fields are not uniform!')
sep = line[4:5] # 获取分割字符
df = pd.read_csv(file, sep=sep)
for row in df.itertuples(index=True, name="Pandas"):
# print(row)
res.append({
'time': getattr(row, 'time'),
'lon': getattr(row, 'lon'),
'lat': getattr(row, 'lat'),
'alti': getattr(row, 'alti'),
'line': getattr(row, 'line')
})
# res = json.dumps(res)
return res
标签:读取,sep,Python,res,getattr,file,line,pandas,row
From: https://www.cnblogs.com/jessecheng/p/17846606.html