下面的代碼片段比較兩個csv文件併合並它們。我的問題是第二個文件以新行打印。避免python輸出中的換行
import csv
import dateutil.parser
with open('a.csv', 'r') as f1:
feed = f1.readlines()
with open ('b.csv', 'r') as f2:
for line in f2.readlines()[1:]:
line = line.split(',')
ts = dateutil.parser.parse(line[3])
print(ts)
for i, log in enumerate(feed):
ls = log.split(',')
ts_start = dateutil.parser.parse(ls[0])
ts_end = dateutil.parser.parse(ls[1])
if (ts >= ts_start) and (ts < ts_end):
print(ts, ts_start, ts_end)
name, tags, mean = line[0], ','.join(line[1:3]),line[-1]
feed[i] = ','.join([log, name, tags, mean])
with open('c.csv', 'w') as f:
f.writelines(feed)
文件:
2015-11-04T13:35:18.657Z,2015-11-04T13:47:06.588Z,load,INSERT
2015-11-04T13:47:47.164Z,2015-11-04T14:07:13.230Z,run,READUPDATE
文件B:
name,tags,time,mean
memory_value,"type=memory,instance=buffered",2015-11-04T13:35:00Z,
memory_value,"type=memory,instance=buffered",2015-11-04T13:45:00Z,1.32
memory_value,"type=memory,instance=buffered",2015-11-04T14:05:00Z,1.11
輸出:
A1,A2,A3,A4,
A5
B1,B2,B3,B4,
B5,
預期輸出:
A1,A2,A3,A4,A5
B1,B2,B3,B4,B5
我該如何實現這個目標?
感謝
「輸出」,你的意思是「c.csv的內容」,或「什麼打印到標準輸出」? – Kevin
@凱文在c.csv。 – arazx
您能否提供a.csv和b.csv的樣本副本? – Kevin