python - 如何对多个文本文件进行数据清理
问题描述
我有包含日志文件的目录。因此,为了阅读和连接,我使用以下命令:
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: ","")
但是Date
并Batch no
根据读取的第一个日志文件在 csv 中显示.....需要帮助以正确方式显示日期和批号。
任何帮助将非常感激。提前致谢
解决方案
推荐阅读
- python - Python - 打开带有空格的文件时遇到问题
- android - 使用 Jetpack 传输数据
- javascript - 如何在 typescript 中使用没有额外 div 的 createPortal?
- ruby-on-rails - ActiveRecord 从多态关联中抓取共享模型
- firebase - Firebase 数据库的访问控制
- javascript - React DOM - 具有多个表单选择一个选项后
- r - 如何在 Markdown 中自动将事后比较添加到我的统计表中?我更喜欢使用 arsenal 包中的 tableby 函数
- bash - Cannot type password in Azure Power Shell
- f# - 无法将无效的 UTF-8 JSON 文本转码为 UTF-16 字符串
- python - 无法单击按钮或使用 Selenium 提交表单