Python 从CSV列表中选择入表
CSV文件的python列表必须从pyodbc加载到Access中。我不明白如何编写SQL字符串来容纳变量,而不是显式地定义INTO表和FROM CSV文件 单个CSV的完整功能SQL语句如下所示:Python 从CSV列表中选择入表,python,ms-access,pyodbc,Python,Ms Access,Pyodbc,CSV文件的python列表必须从pyodbc加载到Access中。我不明白如何编写SQL字符串来容纳变量,而不是显式地定义INTO表和FROM CSV文件 单个CSV的完整功能SQL语句如下所示: strSQL = "SELECT * INTO [TableName] FROM [text;HDR=Yes;FMT=Delimited(,);" + "Database=C:\Path\To\Folder].first.csv;" 是否可以修改此语句以容纳表示要导入ie的CSV的变量
strSQL = "SELECT * INTO [TableName] FROM
[text;HDR=Yes;FMT=Delimited(,);" +
"Database=C:\Path\To\Folder].first.csv;"
是否可以修改此语句以容纳表示要导入ie的CSV的变量,包括导入[TableName]和从数据库
我知道这是一种类似的形式:
strSQL ="SELECT * INTO ? FROM Database=?",[csv_string, csv]
但是引用数据库的复杂FROM语句让我挠头
# DATABASE CONNECTION
access_path = "C:\Path\To\Access\\DB.mdb"
con = pyodbc.connect("DRIVER={{Microsoft Access Driver
(*.mdb,*.accdb)}};DBQ={};".format(access_path))
for csv in csv_list:
# RUN QUERY
strSQL = "SELECT * INTO [TableName] FROM
[text;HDR=Yes;FMT=Delimited(,);" +
"Database=C:\Path\To\Folder].first.csv;"
cur = con.cursor()
cur.execute(strSQL)
con.commit()
con.close()
为什么不像上面那样使用.format呢
您可以执行以下操作:
table=TableName
数据库=C:\Path\To\文件夹
strSQL=
从中选择*进入[{}]
[文本;HDR=Yes;FMT=Delimited,;
{}.first.csv;
.formattable,数据库
也可以使用以下格式:
strSQL=fSELECT*INTO[{table}]FROM[text;HDR=Yes;FMT=Delimited,;{database}].first.csv;
由于选择了答案,以下是正确工作的最终格式:
strSQL = "SELECT * INTO [{}] FROM [text;HDR=Yes;FMT=Delimited(,);".format(csv_str) + \
"Database=C:\PathToFolder].{};".format(csv)
我相信您可以使用Python将CSV文件加载到Access中,但为什么要费事呢。只需使用Access导入CSv文件,不会为您自己创建大量不必要的工作 将CSV文件导入到单独的表中:
Function DoImport()
Dim strPathFile As String
Dim strFile As String
Dim strPath As String
Dim strTable As String
Dim blnHasFieldNames As Boolean
' Change this next line to True if the first row in CSV worksheet
' has field names
blnHasFieldNames = True
' Replace C:\Documents\ with the real path to the folder that
' contains the CSV files
strPath = "C:\Users\Excel\Desktop\test\"
' Replace tablename with the real name of the table into which
' the data are to be imported
strFile = Dir(strPath & "*.csv")
Do While Len(strFile) > 0
strTable = Left(strFile, Len(strFile) - 4)
strPathFile = strPath & strFile
DoCmd.TransferText acImportDelim, , strTable, strPathFile, blnHasFieldNames
' Uncomment out the next code step if you want to delete the
' EXCEL file after it's been imported
' Kill strPathFile
strFile = Dir()
Loop
End Function
或者
将CSV文件导入到一个表中:
Private Sub Command1_Click()
Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String, strBrowseMsg As String
Dim blnHasFieldNames As Boolean
' Change this next line to True if the first row in EXCEL worksheet
' has field names
blnHasFieldNames = False
strBrowseMsg = "Select the folder that contains the CSV files:"
strPath = "C:\Users\Excel\Desktop\Coding\LinkedIn\Import all CSV Files into Different Access Tables\"
If strPath = "" Then
MsgBox "No folder was selected.", vbOK, "No Selection"
Exit Sub
End If
' Replace tablename with the real name of the table into which
' the data are to be imported
strTable = "tablename"
strFile = Dir(strPath & "\*.csv")
Do While Len(strFile) > 0
strPathFile = strPath & "\" & strFile
DoCmd.TransferText acImportDelim, , strTable, strPathFile, blnHasFieldNames
' Uncomment out the next code step if you want to delete the
' EXCEL file after it's been imported
' Kill strPathFile
strFile = Dir()
Loop
End Sub