I have 32 CSV files and each csv file has different fields but i need 8 colums those 8 are same in all csv files. I have tried so many ways but i didn't find any way. Please help me out in this
Asked
Active
Viewed 519 times
0
-
1This question is to broad. Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). Also provide an example to make your question more clear [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Matthijs Mar 30 '19 at 10:56
1 Answers
0
Something like this should get you started:
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]

Mr Ed
- 83
- 2
- 12
-
this is working for me. If I want to import all 32 CSV files and 8 columns in Mysql. How can i do – Anusha Polimera Mar 31 '19 at 17:18
-
Oh, I didn't read your title about MySQL, there's several existing answers, see [possible duplicate](https://stackoverflow.com/questions/10154633/load-csv-data-into-mysql-in-python) or others. Basically, in the "for row in csvreader" loop add an insert statement like "cur.execute ("INSERT INTO my_table (col1,col2,col3,col4,col5,col6,col7,col8) VALUES (%s, %s, %s, %s, %s, %s, %s, %s),row)" . – Mr Ed Apr 01 '19 at 07:17