Sep-01-2022, 11:52 PM
(This post was last modified: Sep-01-2022, 11:52 PM by Pedroski55.)
There was a mistake in line 11
Line 11 should be:
df = pd.read_csv(csv)
This very quickly converted 3 csv files to Excel.
Then I can import the csv into MySQL.
Quote:>>> myApp()
/home/pedro/myPython/pandas/csv_files/poll_count20BECW.csv
Traceback (most recent call last):
File "/usr/lib/python3.8/idlelib/run.py", line 559, in runcode
exec(code, self.locals)
File "<pyshell#2>", line 1, in <module>
File "<pyshell#1>", line 11, in myApp
NameError: name 'csv_file' is not defined
Line 11 should be:
df = pd.read_csv(csv)
This very quickly converted 3 csv files to Excel.
def myApp(): # save all .csv as .xlsx import pandas as pd import glob savepath = '/home/pedro/myPython/pandas/csv_files/' files = glob.glob(savepath + '*.csv') for csv in files: print(csv) df = pd.read_csv(csv) name = csv.split('.') df.to_excel(name[0] + '.xlsx', index=False)
Output:>>> myApp()
/home/pedro/myPython/pandas/csv_files/poll_count20BECW.csv
/home/pedro/myPython/pandas/csv_files/Log.csv
/home/pedro/myPython/pandas/csv_files/poll_count20BECW (1).csv
>>>
I often do this the other way round: use pandas to create an Excel, then use pandas to convert that to csv.Then I can import the csv into MySQL.