2016-12-22 2 views
0

Я нашел this code, чтобы разбить CSV-файл, используя python.Добавить столбцы в csv с помощью запроса python для данных

Мне нужно разделить 3 000 000 записей CSV-файла при изменении столбца A. Мне также нужно добавить еще 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 

Работает отлично!

Смежные вопросы