首页 > 解决方案 > 如何对多个文本文件进行数据清理

问题描述

我有包含日志文件的目录。因此,为了阅读和连接,我使用以下命令:

filenames = glob('*.log')
df = [pd.read_csv(f) for f in filenames

每个日志文件如下所示:

Tracer: (1) 18F-Nb25    Batch no: 3459  Date: 2020-01- 3


Time       IS current   IS volt.    Dee RF  Magnet  Probe   Coll-l  Foil Target Coll-r  Vacuum
05:25:39    0   0   0.0 0.0 130.85  0.1 0.1 0.0 0.1 0.1 2.2E-06
05:25:40    0   0   0.0 0.0 130.85  0.1 0.1 0.0 0.1 0.1 2.2E-06
05:25:41    0   0   0.0 0.0 130.85  0.1 0.1 0.0 0.1 0.1 2.2E-06
05:25:42    0   0   0.0 0.0 130.85  0.1 0.1 0.0 0.1 0.1 4.2E-06

对于数据清理和转换,我正在使用:

fline=open("abc.csv", ).readline().rstrip()

输出:

'Tracer: (1) 18F-Nb25 \tBatch no: 3451 \tDate: 2020-01- 2,Tracer: (1) 18F-Nb25 \tBatch no: 3452 \tDate: 2020-01- 2,Tracer: (1) 18F-Nb25 \tBatch no: 3453 \tDate: 2020-01- 2,Tracer: (1) 18F-Nb25 \tBatch no: 3454 \tDate: 2020-01- 2,Tracer: (1) 18F-Nb25 \tBatch no: 3455 \tDate: 2020-01- 2,Tracer: (1) 18F-Nb25 \tBatch no: 3456 \tDate: 2020-01- 3,Tracer: (1) 18F-Nb25 \tBatch no: 3457 \tDate: 2020-01- 3,Tracer: (1) 18F-Nb25 \tBatch no: 3458 \tDate: 2020-01- 3'

之后,用于拆分:

fline = fline.split('\t')

最后:

df = pd.read_csv('abc.csv',  sep='\t', skiprows=[0,1,2,3], names=['Time','IS current','IS volt.','Dee','RF','Magnet','Probe','Coll-l','Foil','Target','Coll-r', 'Vacuum'])

df['Date'] = fline[2].replace("Date: ","")
df['Batch'] = fline[1].replace("Batch no: ","") 

但是DateBatch no根据读取的第一个日志文件在 csv 中显示.....需要帮助以正确方式显示日期和批号。

任何帮助将非常感激。提前致谢

标签: pythonpandastext-filesdata-cleaninglogfile

解决方案


推荐阅读