Python札记人工智能/模式识别/机器学习精华专题呆鸟的Python数据分析

Python札记48_操作数据

2019-07-19  本文已影响4人  皮皮大

程序在执行的过程中会产生一些数据,一般情况下会将数据保存在磁盘中。其中最简单的方法就是将数据写入到某个文件中,就是将存储的对象格式化(或者叫做序列化)。

将数据存入文件


pickle模块

# 写入数据
import pickle
list1 = [1,2,3,4,5]  # 需要存入的对象
f = open("file1.txt", "wb")  # 存入的文件和格式
pickle.dump(list1, f)  # 调用pickle模块
f.close()  
# 读取数据
list1 = pickle.load(open("file1.txt", "rb"))
list1

image.png

shelve模块

# 写入数据
import shelve
s = shelve.open("file2.txt")   # 写入文件名
s["name"] = "shenzhen"  # 写入的各种数据
s["language"] = "python"
s["age"] = 28
s["contents"] = {"first": "base knowledge", "second": "day day up"}
s.close()
# 读取数据
s = shelve.open("file2.txt", writeback=True)  # writeback参数表示允许向数据中添加新的数据
name = s["name"]
print(name)
age = s["age"]
print(age)
contents = s["contents"]
print(contents)

# 结果
shenzhen
28
{'first': 'base knowledge', 'second': 'day day up'}
for k in s:
    print(k,s[k])

# 结果
name shenzhen
language python
age 28
contents {'first': 'base knowledge', 'second': 'day day up'}

操作数据库

三种主流的数据库:

MySQL数据库

MySQL属于关系型数据库。数据库表是一系列二维数组的集合,用来代表和存储对象之间的关系,由纵向的列和横向的行组成。MySQL数据端口是3306Ubuntu中安装MySQL数据库的安装步骤见笔者札记
MySQL札记1_ubuntu18.04安装MySQL5.7

sudo apt-get install mysql
mysql -uroot -p   # 进入mysql
show databases;  # 查看数据库
use msyql;  # 选中mysql数据库  
show tables;  # 查看表

pymysql模块

安装

sudo pip3 install PyMySQL
import pymysql

创建数据库

create database bookdb character set utf8;   # 创建bookdb数据库;指定编码格式为utf8,中文不会乱码

连接pymysql
使用模块的connect()进行连接:

import pymysql
conn = pymysql.connect(host=“localhost”, port=3306, user="root", passwd="123456", db="bookdb", charset="utf8")

操作数据库

Python建立了和数据库的连接,实际上就是建立了一个pymysql.connect()的实例对象,或者称之为连接对象。Python就是通过连接对象和数据库进行对话。pymysql.connect()实例对象的方法有:

游标对象cur的操作方法

名称 描述
close() 关闭游标
execute 执行一条SQL语句,可以带上参数;
执行之后需要conn.commit(),数据库中才会真正建立
fetchone 返回一条语句
fetchall 返回所有的语句
fetchmany 返回many条语句
nextset() 移动到下一个结果
import pymysql    # 导入模块
conn = pymysql.connect(host=“localhost”, port=3306, user="root", passwd="123456", db="bookdb", charset="utf8")      # 建立python和数据库的连接
cur = conn.cursor()  # 建立游标对象
cur.execute("insert into users (username, passwd, email) values (%s,  %s, %s)", ("python", "123456", "python@gmail.com"))   # 需要再执行conn.commit()

cur.executemany("insert into users (username, passwd, email) values (%s,  %s, %s)", (("python", "123456", "python@gmail.com"), ("java", "456789", "java@gmail.com"), ("php", "123789", "php@gmail.com")))
>>cur.execute("select * from users")
>>lines = cur.fetchall()   # 返回所有的查询结果
>>for line in lines:
    print(line)

# 只想返回一条结果
>>cur.execute("select * from users where id=1")  # 查询id=1

>>cur.execute("select * from users")
>>cur.fetchone()   # 返回一条结果;游标会跟着移动,直到将所有的数据取完
>>cur.fetchone() 
>>cur.fetchone() 
>>cur.scroll(2)  # 相对于当前位置移动
>>cur.fetchone()  # 显示数据
>>cur.scroll(2, "absolute")   # 加上参数,实现“绝对移动”,到第三条

绝对移动的数字不能是负数,相对移动可以是负数

Python的连接对象的游标方法中提供一个参数,将读取到的数据保存成字典形式:

>>cur = conn.cursor(pymysql.cursors.DictCursor)
>>cur.execute("select * from users")
>>cur.fetchall()

更新

>>cur.execute("update users set username=s% where id=2", ("mypython"))   # 更新第二条语句
>>cur.execute("select * from users where id=2")
>>cur.fetchone()
上一篇 下一篇

猜你喜欢

热点阅读