gpt4 book ai didi

python - 将 TXT 转换为 CSV python

转载 作者:太空宇宙 更新时间:2023-11-04 09:45:12 24 4
gpt4 key购买 nike

我有一个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

最佳答案

with open(filename) as infile, open('outfile.csv','w') as outfile: 
for line in infile:
outfile.write(line.replace(' ',','))

关于python - 将 TXT 转换为 CSV python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50101543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com