我有一个txt数据。它看起来如下
time pos
0.02 1
0.1 2
...
Run Code Online (Sandbox Code Playgroud)
等等。所以每一行都用空格隔开。我需要将其转换为 CSV 文件。喜欢
time,pos
0.02,1
0.1,2
0.15,3
Run Code Online (Sandbox Code Playgroud)
我怎样才能用 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
Run Code Online (Sandbox Code Playgroud)
小智 5
with open(filename) as infile, open('outfile.csv','w') as outfile:
for line in infile:
outfile.write(line.replace(' ',','))
Run Code Online (Sandbox Code Playgroud)