我希望能夠翻轉(zhuǎn) csv 文件行但保留標(biāo)題。當(dāng)前的timestamp open high low close volume2020-04-14 2805.1001 2851.8501 2805.1001 2846.0601 31133887412020-04-13 2782.4600 2782.4600 2721.1699 2761.6299 5274310000 .....2000-01-04 1455.2200 1455.2200 1397.4301 1399.4200 10090000002000-01-03 1469.2500 1478.0000 1438.3600 1455.2200 931800000期望的結(jié)果timestamp open high low close volume2000-01-03 1469.2500 1478.0000 1438.3600 1455.2200 9318000002000-01-04 1455.2200 1455.2200 1397.4301 1399.4200 1009000000.....2020-04-13 2782.4600 2782.4600 2721.1699 2761.6299 52743100002020-04-14 2805.1001 2851.8501 2805.1001 2846.0601 3113388741我已經(jīng)能夠用這個(gè)翻轉(zhuǎn)數(shù)據(jù),但我不能保留標(biāo)題。添加了跳過(guò)標(biāo)題,否則將標(biāo)題放在 csv 文件的底部。當(dāng)前代碼 def reverse_csv(): with open("spx.csv", "r") as infile, open("spx_cleaned.csv", "w") as outfile: reader = csv.reader(infile) next(reader, None) # skip the headers writer = csv.writer(outfile) for row in reversed(list(reader)): # process each row writer.writerow(row)謝謝!
添加回答
舉報(bào)
0/150
提交
取消