Python读取CSV和解析json格式数据
1读写CSV⽂件
1. 原始CSV⽂件数据
图1:股票数据stocks.csv
2将股票数据读取为元组序列
代码:
import csv
with open('stocks.csv') as f:
f_csv = ader(f)
headers = next(f_csv)
print(headers)
for row in f_csv:
print(row)# type:list
读取出第⼀⾏为headers,剩下⼀⾏⼀⾏读取为⼀个个list。可以⽤切⽚来进⾏选择⾃⼰想要的数据。但是:使⽤元组切⽚时候很可能混乱,下⾯提供了另⼀种可能的⽅案。
3命名元组
代码:
from collections import namedtuple
with open('stocks.csv') as f:
f_csv = ader(f)
headings = next(f_csv)
Row = namedtuple('Row', headings)
for r in f_csv:
row = Row(*r)
print(row.Symbol, row)# 可直接通过row.Date等获取相应的元素。
4将数据读取为字典列表
代码:
with open('stocks.csv') as f:
f_csv = csv.DictReader(f)
for row in f_csv:
print(row['Symbol'], row)
注:第⼀列需要时表头,这样可以像字典⼀样通过表头关键字获取对应⾏的元素。
5写⼊CSV⽂件
数据格式1:
headers = ['Symbol', 'Price', 'Date', 'Time', 'Change', 'Volume']
rows = [('AA', 39.12, '2017/07/07', '9:36am', -0.18, 181800),
('AB', 49.12, '2017/07/07', '9:36am', -0.48, 281800),
('AC', 319.12, '2017/07/07', '9:36am', -0.28, 481800)]
with open('wstocks.csv', 'w') as f:
f_csv = csv.writer(f)
f_csv.writerow(headers)# 写⼊⼀⾏
f_csv.writerows(rows)# ⼀次性写⼊多⾏
java语言程序设计与数据结构第十一版答案
数据格式2:
headers = ['Symbol', 'Price', 'Date', 'Time', 'Change', 'Volume']
rows = [{'Symbol':'AA', 'Price':39.12, 'Date':'2017/07/07',
'Time':'9:36am', 'Change':-0.18, 'Volume':181800},
{'Symbol':'AB', 'Price':49.12, 'Date':'2017/07/07',
'Time':'9:36am', 'Change':-0.48, 'Volume':281800},
{'Symbol':'AC', 'Price':319.12, 'Date':'2017/07/07',
'Time':'9:36am', 'Change':-0.28, 'Volume':481800}]
with open('wstocks.csv', 'w') as f:
f_csv = csv.DictWriter(f, headers)
f_csv.writeheader()# 写⼊头
c for和foreach的区别f_csv.writerows(rows)# ⼀次性写⼊多⾏
注:写⽂件有个缺陷就是结尾有换⾏符。只需要在打开⽂件的时候加上⼀个参数就OK了:with open('wstocks.csv', 'w', newline='') as f。
6.我们的读取代码
1. 很多时候我们根据实际需要⼤概这样读取CSV⽂件:
import re
with open('wstocks.csv', 'r') as f:
header = False # 是否打印headerTrue—打印
while True:
line = f.readline()
if header:
header = False
continue
if line:
line = re.sub('[\r\n]','',line)
lines = line.split(',')
print(lines)
else:
break
注:这样分割然后⽤索引对数据操作有时候会⽐较⿇烦,⽐如某⼀个数据⾥⾯包含⼀个逗号,有的是被引号括起来的,这些需要⾃⼰去去除的。
1. 当然我们也可以读取其他分隔符的⽂件:只需要改⼀⾏代码就OK:⽐如制表符分割的⽂件读取:
with open('wstocks.csv', ‘r’) as f:
f_csv = ader(f, delimiter=’\t’)
1. 还有⼀种情况需要注意的是,有时候表头⾥⾯可能含有Python⾥⾯的⾮法标志符,这时候就需要⽤正则表达式把⾮法标志符过滤掉。例如:
数据:
mysql分页查询越来越慢处理:
import re
from collections import namedtuple
with open('geo.csv') as f:
f_csv = ader(f)
headers = [re.sub('[^a-zA-Z_]', '_', h) for h in next(f_csv)]
Row = namedtuple('Row', headers)
print(headers)
for r in f_csv:
print(Row(*r))
华为手机铃声spring下载
1. CSV模块不会尝试去解释数据或者将数据转换为出字符串之外的类型,这时候就需要我们⼿动操作了。看代码1:
col_types = [str, float, str, str, float, int]
with open('stocks.csv') as f:
f_csv = ader(f)
headers = next(f_csv)
for row in f_csv:
print(row)# 转换前
row = [convert(value) for convert, value in zip(col_types, row)]
print(row) # 转换后
看代码2:
print("Reading as dicts with type conversion")
field_types = [('Price', float),
('Change', float),
('Volume', int)]
with open('stocks.csv') as f:
for row in csv.DictReader(f):
print(row) # 转换前
python请求并解析json数据
row.update((key, conversion(row[key])) for key, conversion in field_types)
print(row) # 转换后
2读写JSON数据
1将数据结构转换为json
实例:
import json
data = {
'name':'ACME',
'shares':100,
'price':542.23}
json_str = json.dumps(data) # <class 'str'>
print(json_str)
2把json编码的字符串转换为对应的数据结构
data = json.loads(json_str) # <class 'dict'>
print(type(data), data)
3Json-Python相互转化对照表sql server数据库卸载
Python-----------àJson Json -----------àJson
dict object object dict list, tuple array array list
str, unicode string string unicode int, long, float number number (int)int, long True true number (real)float False false true True None null false False
看⼏个例⼦吧:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。