使用这个站点,我可以从 CSV 文件中读取随机行,但是当我将这些随机行写入不同的 csv 文件时,我会丢失一些格式。这就是我的意思:
我正在阅读的 csv 文件的片段:
案例,事件,P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12,P13,P14,P15,P16,P17,P18,P19,P20,P21,P22,P23, P24,P25,P26,P27,P28,P29,P30 C000167,E03468,58,10,81,-0.3406615026224174,-5.581162038780728E-4,0.650243470511574,0.9011615038586112,0.829330406449827,-0.7616995269258996,-0.0019909209274398174,-0.025834173961019672,,,, ,,,,,,,,,,,,,,30, C000167,E03468,58,10,81,-0.3406615026224174,-5.581162038780728E-4,0.6517651862747824,0.9074693464398635,0.8380469158984926,-0.7589916620900861,-0.0019954144276356335,-0.03222025640621825, ,,,,,,,,,,,,,,,,,30, C000167,E03468,58,10,81,-0.3468052064483135,-5.581162038780728E-4,0.6522422131598,0.9025930670926058,0.8332814044565442,-0.7508680675826261,-0.001998391716108935, -0.03533445710480293,,,,,,,,,,,,,,,,,,30,
从上面的文件中读取 N 个随机行后,我得到的是: 输出文件的片段:
"案例,事件,P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12,P13,P14,P15,P16,P17,P18,P19,P20,P21,P22,P23 ,P24,P25,P26,P27,P28,P29,P30 " "C001685,E35590,58,10,81,-0.3654481697820668,-5.581162038780728E-4,0.655619563505728,0.8856242052995037,0.7811768584356523,-0.6824944804781753,-0.0018236965581893878,0.08998255581671266,0.46818235899722466 ,0.5349178991074381,0.0023192678347080547,0.0023192853642003794,-1.379960600462541E-4,-0.0029589652839934494,0.0190440944409012,-0.006310411153684246,0.2667578790879315,0.8519823892954023,0.005696496533938292,0.6083983010037551,-0.15781372752517367,0.8433075356454734,0.8147766325543522,0.6320612846744368,-0.47415970209573,34,1.521461493699171 "
注意我得到的报价。当我读取输出文件时,原来的 32 列现在减少到 1 列。
我使用的代码如下:
import random
import csv
import os
os.remove("train_select.csv")
file_size=1146880-600
f=open("train.csv",'rb')
dialect = csv.Sniffer().sniff(f.readline())
f.seek(0)
csv_file=open("train_select.csv",'wb')
case_writer = csv.writer(csv_file,dialect)
f.seek(0)
random_line=f.readline()
case_writer.writerow([random_line])
for i in range(0,20):
offset=random.randrange(file_size)
f.seek(offset)
f.readline()
random_line=f.readline()
case_writer.writerow([random_line])
f.close()
csv_file.close()