Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python将导入/批量加载到MS Access中_Python_Ms Access_Bulkinsert - Fatal编程技术网

使用python将导入/批量加载到MS Access中

使用python将导入/批量加载到MS Access中,python,ms-access,bulkinsert,Python,Ms Access,Bulkinsert,有人知道使用python将几个CSV文件加载到一个给定的访问表中的简单方法吗 例如,我的目录可能有100个名为import_u*.csv的文件(import_1.csv、import_2.csv等) MS Access中有一个目标表应接收所有这些csv 我知道我可以使用pyodbc并逐行构建语句来完成这项工作,但这需要大量的编码。然后,还必须使SQL保持最新,因为字段可能会被添加或删除。MS Access有自己的批量加载功能——我希望这可以通过python访问,或者python有一个库来实现同样

有人知道使用python将几个CSV文件加载到一个给定的访问表中的简单方法吗

例如,我的目录可能有100个名为import_u*.csv的文件(import_1.csv、import_2.csv等)

MS Access中有一个目标表应接收所有这些csv

我知道我可以使用pyodbc并逐行构建语句来完成这项工作,但这需要大量的编码。然后,还必须使SQL保持最新,因为字段可能会被添加或删除。MS Access有自己的批量加载功能——我希望这可以通过python访问,或者python有一个库来实现同样的功能

如果有一个图书馆可以像以下一样轻松地完成这项工作,我会非常高兴:

dbobj.connectOdbc(dsn) dbobj.bulkLoad(“MyTable”、“c:/temp/test.csv”)

在内部,需要一些工作来找出模式并使其工作。但希望有人已经完成了这项繁重的工作


有办法进行批量导入吗?阅读熊猫的内容已经足够琐碎了,但是你必须从那里将其导入MS Access

这是一篇老文章,但我要尝试一下。因此,您在一个目录中有100多个CSV文件,您希望将所有内容都推送到MS Access中。好的,我会在Python中将所有CSV文件合并成一个sight DF,然后保存DF,并将其导入MS Access

#1 Use Python to merge all CSV files into one single dataframe:
# Something like...

import pandas as pd
import csv
import glob
import os

#os.chdir("C:\\your_path_here\\")
results = pd.DataFrame([])
filelist = glob.glob("C:\\your_path_here\*.csv")
#dfList=[]
for filename in filelist:
    print(filename)  
    namedf = pd.read_csv(filename, skiprows=0, index_col=0)
    results = results.append(namedf)

results.to_csv('C:\\your_path_here\\Combinefile.csv')
或者,我也会这样做……在Access中使用VBA将所有CSV文件合并到一个表中(无论如何,Python都不需要)


此处已显示从熊猫到Access的导出。泰。这些例子是我不想做的。为了清晰起见,让我编辑一下我的文章。我所说的批量加载是指无需定制SQL代码就可以轻松地将表导入Access。我从来没有找到另一条路…我只是给了你一条路。你真的试过了吗?我不这么认为。
Private Sub Command1_Click()

    Dim strPathFile As String, strFile As String, strPath As String
    Dim strTable As String
    Dim blnHasFieldNames As Boolean

    ' Change this next line to True if the first row in EXCEL worksheet
    ' has field names
    blnHasFieldNames = False

    ' Replace C:\Documents\ with the real path to the folder that
    ' contains the CSV files
    strPath = "C:\your_path_here\"

    ' 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
          DoCmd.TransferText acImportDelim, , strTable, strPathFile, True

    ' 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