2017-11-08 10 views
0

大規模なcsvファイルを作成し、列内の特定の単語に基づいて複数のcsvファイルに分割するコードを作成しました。元のcsvファイルには文字列のフィールドがいくつかあり、その周りに引用符が付きます。例えば文字列を引用符で囲んでcsvを書く(Python)

Field1,Field2,Field3,Field4 
1,2,"red",3 
1,4,"red",4 
3,4,"blue",4 

など

私のコードは、フィールド4に基づいて、別のCSVにファイルを分割します。

私の出力は次のようになります。

3.csv 
Field1,Field2,Field3,Field4 
1,2,red,3 

4.csv 
Field1,Field2,Field3,Field4 
1,4,red,4 
3,4,blue,4 

私は、ファイルが文字列は、それらの周りに引用符を持っている場合にのみ動作するソフトウェアの一部に供給されている私の出力は、フィールド3に、文字列の前後に引用符を維持したいです、それは非常に迷惑です。

私の現在のコードは次のようになります。

import csv 

#Creates empty set - this will be used to store the values that have already been used 
newfilelist = set() 

#Opens the large csv file in "read" mode 
with open('File.csv, 'r') as csvfile: 

    #Read the first row of the large file and store the whole row as a string (headerstring) 
    read_rows = csv.reader(csvfile) 
    headerrow = next(read_rows) 
    headerstring=','.join(headerrow) 
    for row in read_rows: 

     #Store the whole row as a string (rowstring) 
     rowstring=','.join(row) 

     #Takes Field 4 
     newfilename = (row[3]) 


     #This basically makes sure it is not looking at the header row. 
     if newfilename != "field4": 


      #If the newfilename is not in the newfilename set, add it to the list and create new csv file with header row. 
      if newfilename not in newfilelist:  
       newfilelist.add(newfilename) 
       with open('//output/' +str(newfilename)+'.csv','a') as f: 
        f.write(headerstring) 
        f.write("\n") 
        f.close()  
      #If the newfilename is in the newfilelist set, append the current row to the existing csv file.  
      else: 
       with open('//output/' +str(newfilename)+'.csv','a') as f: 
        f.write(rowstring) 
        f.write("\n") 
        f.close() 

誰もが、文字列の前後に引用符を取得する方法を私に助言することはできますか?残念なことに私のファイルを使用するソフトウェアでは、この形式にする必要があります!

+0

おそらくこのhttps://stackoverflow.com/questions/36628847/keep-double-quotes-in-a-text-file-using-csv-reader –

答えて

0

合格quoting=csv.QUOTE_NONNUMERIC~csv.writer()

0

CSVwriterは、あなたがしようとしていることに対して過度のものかもしれません。行全体を変更しない場合は、行全体を書き込んでください。

#Creates empty array - this will be used to store the values that have already been used 
newfilelist = {} 

#Opens the large csv file in "read" mode 
with open('File.csv, 'r') as csvfile: 

    #Read the first row of the large file and store the whole row as a string (headerstring) 
    headerstring = csvfile.readline() 
    for row in csvfile.readlines(): 

     #Takes Field 4 
     newfilename = row.split(',')[3].strip('"') 

     #If the newfilename is not in the newfilename set, add it to the list and create new csv file with header row. 
     if newfilename not in newfilelist:  
      newfilelist[newfilename] = open('//output/' +str(newfilename)+'.csv','w'): #open a file and store the file reference in an dictionary 
      newfilelist[newfilename].write(headerstring) 

     newfilelist[newfilename].write(row) # Write out a row to an existing file 

#Close all open files 
for k in newfilelist.keys(): 
    newfilelist[k].close() 
+0

のDUPは、このコードは、ファイル場合は壊れているようですサイズは巨大ですが、初期コードが機能します。私はMemoryErrorを "for行csvfile.readlines():"行に取得します。 – Actuary

+0

あなたの出力ロジックがたくさんのファイルを生成するのであれば、驚くべきことではありません。ファイルに書き込まなくなった時点を知っていれば、最後まで待つのではなく、早期に終了することができます。 – WombatPM

+0

これを行うと思いますが、データを並べ替える必要がありますか?必ずしも注文はなく、何万ものファイルが存在する可能性があります。データの並べ替えは、最初のファイルサイズが複数ギガバイトになると難しいと思います! – Actuary

関連する問題