简体   繁体   English

使用 Python 将目录中的 CSV 文件导入 MySql 表

[英]Importing CSV files from a directory into a MySql table with Python

I would like to read multiple CSV files in a folder/path and put them into a mysql table.. I found several solutions here pointing reading a single csv file into mysql, but I need to parse multiple files. I would like to read multiple CSV files in a folder/path and put them into a mysql table.. I found several solutions here pointing reading a single csv file into mysql, but I need to parse multiple files. Could you help me where can I start?你能帮我从哪里开始吗?

I've used the script below to read csv.. but I am open to your recommendations.我已经使用下面的脚本来阅读 csv.. 但我愿意接受您的建议。

Example single csv file;示例单个 csv 文件;

Columns will be:列将是:

TimeStamp,Message/Event,Server Totals,Client Totals
1564981556,INVITE Requests,39967,37787
1564981556,100 Trying          ,39896,37758
1564981556,180 Ringing         ,1113,1113
1564981556,181 Forwarded       ,0,0
1564981556,182 Queued          ,1,1
1564981556,183 Progress        ,251,251
1564981556,1xx Provisional     ,0,0
1564981556,200 OK              ,913,913
1564981556,202 Accepted        ,0,0
1564981556,2xx Success         ,0,0
1564981556,30x Moved           ,0,0
1564981556,400 Bad Request     ,2,2
1564981556,401 Unauthorized    ,252,252
1564981556,403 Forbidden       ,320,324
1564981556,404 Not Found       ,487,487
1564981556,405 Not Allowed     ,0,0
1564981556,406 Not Acceptable  ,0,0
1564981556,407 Proxy Auth Req  ,998,998
1564981556,408 Request Timeout ,5220,5217
1564981556,415 Bad Media Type  ,0,0
1564981556,423 Too Brief       ,0,0
1564981556,480 Unavailable     ,49,49
1564981556,481 Does Not Exist  ,0,0
1564981556,482 Loop Detected   ,0,0
1564981556,483 Too Many Hops   ,6738,6738
1564981556,484 Address Incompl ,1039,1039
1564981556,485 Ambiguous       ,0,0
1564981556,486 Busy Here       ,159,174
1564981556,487 Terminated      ,2530,2530
1564981556,488 Not Acceptable  ,8199,8199
1564981556,489 Bad Event       ,0,0
1564981556,491 Req Pending     ,0,0
import pandas as pd
import glob

path = r'C:\\Users\\xxx\\Documents\\files\\' # use your path

all_files = glob.glob(path + "\*.csv")

li = []

for filename in all_files:
    df = pd.read_csv(filename, index_col=None, header=0)
    li.append(df)

frame = pd.concat(li, axis=0, ignore_index=True)

print(frame)
import d6tstack
import glob

c = d6tstack.combine_csv.CombinerCSV(glob.glob('*.csv'))
c.to_mysql_combine('mysql+mysqlconnector://usr:pwd@localhost/db', 'tablename'

With the d6stack library, you can store it.!.使用 d6stack 库,您可以存储它。!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM