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!

Hello, I am having trouble uploading a .csv file onto the mySQL hosted server. I DID add LOCAL. It looks like this:

Shall I place the file anywhere else? What if I upload it into the app files? Many thanks, Vanni

Hello, I am having trouble uploading a .csv file onto the mySQL hosted server. I DID add LOCAL. It looks like this:

mysql> LOAD DATA LOCAL INFILE '/home/ra/Scrivania/SinglePurposeCode/WorldBank_CO2.csv';

Shall I place the file anywhere else? What if I upload it into the app files? Many thanks, Vanni

I doubt that file exists on PythonAnywhere. Based on your username, the first part of the that filename needs to be /home/VanniGas. You can only use load data local infile to load a file that exists on the file system where you're running the command.

I have uploaded library.csv file through the below syntax, but I am getting errors. please help me. Thank you wget https://www.pg4e.com/library.csv pg4e_54caa223c1-> \copy track_raw from 'library.csv with delimiter ',' csv; library.csv with delimiter : No such file or directory

Could you format your post so it's visible what is your command, how do you run it and what is the output?