Minimal notes
For the time being, python2.7.x.
Please see here for how to confirm the existence in the first place.
MySQL
It's hard to decide which module to use to communicate with MySQL, but for the time being, use mysql-connector-python.
#coding:utf-8
import mysql.connector
con = mysql.connector.connect(
host='localhost',
db='testdb',
user='root',
password='root'
)
cur = con.cursor(buffered=True)
sql = "select * from members"
cur.execute(sql)
rows = cur.fetchall()
for row in rows:
print row[1]
cur.close()
con.close()
CSV
csv(utf-8)
You don't have to worry about utf-8 comma-separated files.
#coding:utf-8
import csv
file = "test.csv"
f = open(file,"r")
reader = csv.reader(f)
for row in reader:
print row[0]
f.close()
It is common for csv created in Excel to be sent and said "Read!". There is no time to deal with it in detail, but for the time being, you can read it by doing the following.
#coding:utf-8
import csv
file = "test2.csv"
f = open(file,"r")
reader = csv.reader(f)
for row in reader:
print row[0].decode('cp932')
f.close()
For some reason, you may not be able to go without processing the raw file.
The line break at the end of the line is removed with strip ().
#coding:utf-8
f = open('test.csv','r')
for row in f:
item = row.strip().split(',')
print item[0]
f.close()
If you want to use regular expressions, use re.split (pattern, string).
#coding:utf-8
import re
f = open('test.csv','r')
for row in f:
item = re.split(',',row)
print item[0]
f.close()
The rest is related to Pandas. I would like to add it from time to time.
Recommended Posts