[Data science basics] I tried saving from csv to mysql with python

Data science It is essential to set data in RDB.

By the way, the above

-[Python beginners tried data science] Data acquisition from API [day1] -[Basics of data science] Collecting data from RSS with python

For the time being, the series continues. I plan to continue writing analysis with the acquired data in the future. (plans)

This time I will do until I put the data on csv into the table of mysql.

The flow is common because you just put it from csv to db --db set --parse csv --Connect to mysql --Hit sql with csv row data It feels like. I'm a python beginner and I usually enjoy querying with rails, so I tried it while checking each one.

Set user or db used this time for mysql

It's usually basic mysql.

parse csv

import csv

Put the csv module in

with open('path/to/csvfile.csv') as csvfile:
  reader = csv.reader(csvfile)

In `csvfile``` contains ``` <open file'path / to / csvfile.csv', mode'r' at 0x1098da030> `object, and the reader contains csvfile Contains an object that iterates over the line.

Return a reader object which will iterate over lines in the given csvfile. csvfile can be any object which supports the iterator protocol and returns a string each time its next() method is called — file objects and list objects are both suitable.

From here

skip header

I didn't want to include the header in the csvfile, so I skipped the header.

  next(reader, None)

The explanation of next () is here.

next(iterarot[, default]) Retrieve the next item from the iterator by calling its next() method. If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised.

Look at each line in turn

  for row in reader:
		#Put line by line in db

Connect to mysql

import mysql.connector

Put the module in

dbcon = mysql.connector.connect(
  database=inifile.get("database", "db"),
  user=inifile.get("database", "user"),
  password=inifile.get("database", "password"),
  host=inifile.get("database", "host")
)
dbcur = dbcon.cursor()
dbcur.execute('INSERT INTO table name(col name, col name) VALUES(%s, "%s")' % ('v1', 'v2'))
dbcon.commit()

Enter with. If you read the air, you can understand it (sorry, it was troublesome to check the behavior of each module's method). It feels like sql is actually being executed by commit. One of the reasons is probably that it is more efficient to hit sql at once when connecting once rather than disconnecting by connecting mysql many times when there are multiple sqls. (speculation)

All together

Like this. (The table name and config are for my environment this time, so please change them appropriately.)

import mysql.connector
import csv
import ConfigParser

inifile = ConfigParser.ConfigParser()
inifile.read("config/database.ini")
dbcon = mysql.connector.connect(
  database=inifile.get("database", "db"),
  user=inifile.get("database", "user"),
  password=inifile.get("database", "password"),
  host=inifile.get("database", "host")
)
dbcur = dbcon.cursor()
with open('RCdata/chefmozaccepts.csv') as csvfile:
  reader = csv.reader(csvfile)
  next(reader, None)
  for row in reader:
    dbcur.execute('INSERT INTO restaurants_payments_methods (restaurant_id, payment_method) VALUES(%s, "%s")' % tuple(row))
dbcon.commit()

Supplement

Other things I did not know about python.

Python has a built-in tuple surrounded by ().

Difference between tuple and list from here

just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

I didn't know the tuple.

Recommended Posts

[Data science basics] I tried saving from csv to mysql with python
I tried to get CloudWatch data with Python
I tried to touch the CSV file with Python
I tried to analyze J League data with Python
[Basics of data science] Collecting data from RSS with python
I tried to make various "dummy data" with Python faker
Notes on importing data from MySQL or CSV with Python
[Pandas] I tried to analyze sales data with Python [For beginners]
I tried to make a generator that generates a C # container class from CSV with Python
I tried to save the data with discord
I tried to output LLVM IR with Python
I tried to automate sushi making with python
I tried to create CSV upload, data processing, download function with Django
Write CSV data to AWS-S3 with AWS-Lambda + Python
I tried fMRI data analysis with python (Introduction to brain information decoding)
How to scrape image data from flickr with python
I tried to implement Minesweeper on terminal with python
I tried to get started with blender python script_Part 01
I tried to draw a route map with Python
I tried to solve the soma cube with python
I tried to get started with blender python script_Part 02
I tried to implement an artificial perceptron with python
I tried to automatically generate a password with Python3
I tried collecting data from a website with Scrapy
I tried to solve the problem with Python Vol.1
I tried to solve AOJ's number theory with Python
I tried fp-growth with python
I tried scraping with Python
Write to csv with Python
I tried gRPC with Python
I tried scraping with python
I tried to aggregate & compare unit price data by language with Real Gachi by Python
I tried scraping food recall information with Python to create a pandas data frame
I tried to find the entropy of the image with python
I want to be able to analyze data with Python (Part 3)
I tried to simulate how the infection spreads with Python
I tried using the Python library from Ruby with PyCall
I tried various methods to send Japanese mail with Python
I tried sending an email from Amazon SES with Python
I want to be able to analyze data with Python (Part 1)
I want to be able to analyze data with Python (Part 4)
I want to be able to analyze data with Python (Part 2)
[Python] I tried to visualize tweets about Corona with WordCloud
Mayungo's Python Learning Episode 3: I tried to print numbers with print
I tried to make GUI tic-tac-toe with Python and Tkinter
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
I tried to divide the file into folders with Python
I tried to get data from AS / 400 quickly using pypyodbc
I tried to make a function to retrieve data from database column by column using sql with sqlite3 of python [sqlite3, sql, pandas]
I tried to touch Python (installation)
[Python] Write to csv file with Python
Create folders from '01' to '12' with python
Output to csv file with Python
I tried web scraping with python.
I made a server with Python socket and ssl and tried to access it from a browser
[Python / Ruby] Understanding with code How to get data from online and write it to CSV
[Data science basics] Data acquisition from API
I want to debug with Python
Tohoku University 2020 First semester math exam (science) I tried to solve major questions 1 to 3 with Python
I tried running prolog with python 3.8.2.
I tried SMTP communication with Python