使用 python 数据提示将列添加到 csv
Add columns to csv using python prompt for data
我发现 this code 使用 python 拆分 CSV 文件。
当 A 列发生变化时,我需要拆分 3,000,000 条记录的 CSV 文件。
我还需要向 table
添加 2 个字段
- 空白(在每行旁边添加一个逗号)。
- 在最后一个字段中添加日期,但它应该询问我日期。
有人能帮我在这段代码中添加 2 个东西吗?
- 添加更多字段的提示
提示字段中应该有什么
我正在从之前包含的 link 中复制代码
#!/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')
谢谢
用 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
效果很好!
我发现 this code 使用 python 拆分 CSV 文件。
当 A 列发生变化时,我需要拆分 3,000,000 条记录的 CSV 文件。 我还需要向 table
添加 2 个字段- 空白(在每行旁边添加一个逗号)。
- 在最后一个字段中添加日期,但它应该询问我日期。
有人能帮我在这段代码中添加 2 个东西吗?
- 添加更多字段的提示
提示字段中应该有什么
我正在从之前包含的 link 中复制代码
#!/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')
谢谢
用 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
效果很好!