2016-12-22 6 views
0

this codeは、Pythonを使用してCSVファイルを分割しています。データ用のpythonプロンプトを使用してcsvに列を追加する

列Aが変更されたときに、300万レコードのCSVファイルを分割する必要があります。 テーブルにさらに2つのフィールドを追加する必要があります

  1. 空白(各行の隣にコンマを入れてください)。
  2. 最後のフィールドに日付を追加しますが、日付を尋ねる必要があります。

このコードに2つ追加することができますか?

  1. 私はリンクからコードをコピーしています
  2. プロンプトフィールドにどうあるべきか

    複数のフィールドを追加するためのプロンプトは、以前

    #!/usr/bin/env python3 
    import binascii 
    import csv 
    import os.path 
    import sys 
    from tkinter.filedialog import askopenfilename, askdirectory 
    from tkinter.simpledialog import askinteger 
    
    def split_csv_file(f, dst_dir, keyfunc): 
        csv_reader = csv.reader(f) 
        csv_writers = {} 
        for row in csv_reader: 
         k = keyfunc(row) 
         if k not in csv_writers: 
            csv_writers[k] = csv.writer(open(os.path.join(dst_dir, k), 
                  mode='w', newline='')) 
         csv_writers[k].writerow(row) 
    
    def get_args_from_cli(): 
        input_filename = sys.argv[1] 
        column = int(sys.argv[2]) 
        dst_dir = sys.argv[3] 
        return (input_filename, column, dst_dir) 
    
    def get_args_from_gui(): 
        input_filename = askopenfilename(
         filetypes=(('CSV', '.csv'),), 
         title='Select CSV Input File') 
        column = askinteger('Choose Table Column', 'Table column') 
        dst_dir = askdirectory(title='Select Destination Directory') 
        return (input_filename, column, dst_dir) 
    
    if __name__ == '__main__': 
        if len(sys.argv) == 1: 
         input_filename, column, dst_dir = get_args_from_gui() 
        elif len(sys.argv) == 4: 
         input_filename, column, dst_dir = get_args_from_cli() 
        else: 
         raise Exception("Invalid number of arguments") 
        with open(input_filename, mode='r', newline='') as f: 
         split_csv_file(f, dst_dir, lambda r: r[column-1]+'.csv') 
         # if the column has funky values resulting in invalid filenames 
         # replace the line from above with: 
         # split_csv_file(f, dst_dir, lambda r: binascii.b2a_hex(r[column-1].encode('utf-8')).decode('utf-8')+'.csv') 
    

を含まありがとう

答えて

0

VBSで書かれました

basename = "csv_split_" 
hasHeader = True 'Change to False if there is no header. 

argCnt = WScript.Arguments.Count 
If argCnt < 1 Then 
    WScript.Echo "Drag a CSV over this script to edit it." 
    WScript.Quit 
End If 
flnm = WScript.Arguments.Item(0) 

set fs = WScript.CreateObject("Scripting.FileSystemObject") 
set cv = fs.OpenTextFile (WScript.Arguments.Item(0)) 
If Not fs.FileExists(flnm) Or LCase(fs.GetExtensionName(flnm)) <> "csv" Then 
    WScript.Echo "This script is meant for CSV only." 
    WScript.Quit 
End If 
fol = fs.GetParentFolderName(flnm) 
customValue = InputBox("What should the last column contain?", "Column Info") 

Set pat = New RegExp 
pat.Global = True 
pat.IgnoreCase = True 
pat.Pattern = "^(""[^""]*""|[^,]*)?," 
recentCol = "" 
csvCount = 1 
header = "" 
cnt = 0 

While Not cv.AtEndOfStream 
    cnt = cnt + 1 
    row = cv.ReadLine 
    If Right(row,1) <> "," Then: comma = ",": Else: comma = "": End If 
    Set col1 = pat.Execute(row) 
    col = col1.Item(0).Value 
    sameFile = true 
    If recentCol <> "" Then 
     If col <> recentCol And cnt > 2 Then 
      csvCount = csvCount + 1 
      sameFile = false 
     End If 
    Else 
     header = row & comma & """Off Peak"",""Effective Date""" 
     Set csv = fs.OpenTextFile(fol & "\" & basename & csvcount & ".csv", 8, True)  
    End If 
    recentCol = col 
    If Not samefile Then 
     csv.close 
     Set csv = fs.OpenTextFile(fol & "\" & basename & csvcount & ".csv", 8, True) 
    End If 
    If hasHeader And (1 = cnt or Not samefile) Then 
     csv.WriteLine(header) 
    Else 
     csv.WriteLine(row & comma & ",""" & customValue & """") 
    End if 
Wend 

csv.Close 

作品素晴らしいです!

関連する問題