使用python将多个CSV文件导入mysql

时间:2019-03-30 10:53:03

标签: mysql mysql-python

我有32个CSV文件,每个csv文件具有不同的字段,但我需要8个列,所有8个csv文件中的8个都相同。我已经尝试了很多方法,但是没有找到任何方法。请帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

这样的事情应该让您入门:

import csv
import glob
csv_dir = "/Users/Documents/CSVfiles/"
for fcnt,csvfile in enumerate(glob.iglob(csv_dir + '*.csv')):  # iterate through all csv files in this directory
    with open(csvfile, newline='') as f:  # open spreadsheet file, fcnt is a count of files
        csvreader = csv.reader(f, delimiter=' ', quotechar='|') # Each row is returned as a list of strings.
        for row in csvreader:
            col1 = row[0]
            col2 = row[1]
            col3 = row[2]
            col4 = row[3]
            col5 = row[4]
            col6 = row[5]
            col7 = row[6]
            col8 = row[7]