【问题标题】:Add columns to csv using python prompt for data使用 python 提示数据将列添加到 csv
【发布时间】:2016-12-22 15:23:24
【问题描述】:

我发现 this code 使用 python 分割一个 CSV 文件。

当 A 列发生变化时,我需要拆分 3,000,000 条记录 CSV 文件。 我还需要向表中添加另外 2 个字段

  1. 空白(在每行旁边添加一个逗号)。
  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')
    

谢谢

【问题讨论】:

    标签: python-3.x csv export-to-csv


    【解决方案1】:

    用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
    

    效果很好!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-19
      相关资源
      最近更新 更多