我发现this code使用python拆分CSV文件。
当A列发生变化时,我需要拆分3,000,000条记录CSV文件。 我还需要在表格中再添加2个字段
是否有人能够帮助我为此代码添加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 :(得分: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
很棒!