将TXT转换为CSV python [英] converting TXT to CSV python
本文介绍了将TXT转换为CSV python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个txt数据.看起来如下
I have a txt data. it looks as follows
time pos
0.02 1
0.1 2
...
,依此类推.因此,每行之间都用空格隔开.我需要将其转换为CSV文件.像
and so on. so the each line is separated with a space. I need to convert it in to a CSV file. like
time,pos
0.02,1
0.1,2
0.15,3
如何使用python做到这一点?这就是我尝试过的
How can I do it with python ? This is what I have tried
time = []
pos = []
def get_data(filename):
with open(filename, 'r') as csvfile:
csvFileReader = csv.reader(csvfile)
next(csvFileReader)
for row in csvFileReader:
time.append((row[0].split(' ')[0]))
pos.append((row[1]))
return
推荐答案
with open(filename) as infile, open('outfile.csv','w') as outfile:
for line in infile:
outfile.write(line.replace(' ',','))
这篇关于将TXT转换为CSV python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文