【发布时间】:2018-04-30 13:26:12
【问题描述】:
我有一个 txt 数据。它看起来如下
time pos
0.02 1
0.1 2
...
等等。所以每一行都用空格隔开。我需要将其转换为 CSV 文件。喜欢
time,pos
0.02,1
0.1,2
0.15,3
我怎样才能用 python 做到这一点?这是我尝试过的
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
【问题讨论】:
-
你的代码有什么问题?
-
您的文件是否有任何包含空格的引用参数?如果没有,只做
line.replace(' ', ',')就足够了吗?