python3 log提取csv,使用Python将日志文件转换为CSV文件

I have the following problem at work - I need to take a log file with items arranged as follows:

A1

B1

C1

A2

B2

C2

.

.

.

An

Bn

Cn

I need a complete csv file like so:

A1,B1,C1

A2,B2,C2

A3,B3,C3

...

An,Bn,Cn

How can I do this using a python script?

EDIT: Actually, the format is written out in the following way -

Voltage: A1

Current: B1

Power: C1

How can I convert it to -

Voltage, Current, Power

A1,B1,C1

解决方案import csv

with open('myfile.log') as file:

lines = file.read().splitlines()

lines = [lines[x:x+3] for x in range(0, len(lines), 3)]

with open('yourcsv.csv', 'w+') as csvfile:

w = csv.writer(csvfile)

w.writerows(lines)

Note that the dots are still there and they would be treated as values (so they'll be separated by comma's too)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值