Forums

load CSV

can i load csv file into mysql db?

Yes. http://www.mysqltutorial.org/import-csv-file-mysql-table/ and https://help.pythonanywhere.com/pages/LoadDataInfile

How can i do this within a python script rather than a Bash console? I have csv files in /home/user/dir and i want to load them into MySQL in an automated way each day.

@enderwigg I'm sure you can schedule a task. :]

Create a bash file somewhere and put your commands into the file. Then visit the schedule tab and point the location to your file.

Just to expand on what @EndenDragon (correctly!) said -- you can do this from a bash script (don't forget to use chmod +x to make it executable before scheduling it) or, if you prefer, you can write a Python script using the Python csv module to read the file, and the MySQL module of your choice to write to the database. Either of those can be scheduled on the "Schedule" tab -- it's not only Python scripts that can be scheduled.

Hello,

I want to load data unto the web application for the user to read. I had used this code to do so: csv_file = csv.reader(open('http://www.sportstats.com/volleyball/.csv')) next(csv_file). However, as I run this code it gives me a syntax error. How can I solve the problem?

Regards Josiah Gumede

what version of python are you running it with? and also is next(csv_file) in the same line as the previous code?

I am running with version 3.6.

can you show the full error stack trace?

Here is the full error stack trace: File "/home/JosiahVS/mysite/underdogsvolleyball.py", line 5 upsets = 0 non_upset = 0 ^ SyntaxError: invalid syntax

I don't understand this error: File "/home/JosiahVS/mysite/flask_app.py", line 37 export APP_CONFIG_FILE=/var/www/yourapp/config/production.py ^ SyntaxError: invalid syntax

Should I get rid of the underscore so to combine the code or just change the Python I am using?

That looks like you have non-Python code inside a Python file. The line

 export APP_CONFIG_FILE=/var/www/yourapp/config/production.py

...is in the Bash shell script syntax, so Python can't understand it.

Similarly, if you have a line in your file that goes like this:

upsets = 0 non_upset = 0

...then that isn't valid Python -- it looks like it's two lines run together.

I'm trying to import a csv from my files and keep getting the error showed in: https://help.pythonanywhere.com/pages/LoadDataInfile

I opened a bash console, modified the command like so: mysql -h skoyron.mysql.pythonanywhere-services.com -u skoyron 'skoyron$clan' -p --local-infile=1 and if I ENTER, it opens MySQL shell. So i try and import the file like so:

LOAD DATA INFILE 'clandb.csv' 
INTO TABLE clan
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;

but still shows the error :

ERROR 1045 (28000): Access denied for user 'skoyron'@'%' (using password: YES)

Any idea on what I'm doing wrong?

Could you try again, adding "LOCAL" before the "INFILE"?

Thank you Giles, that was exactly what it was missing :)

Excellent -- glad I could help!