JSON格式
- JSON (JavaScript Object Notation)
- JSON语法规则
- 名称必须用双引号括起来
- 值可以是任意的由双引号包括的字符串、数字、布尔值、空值、JavaScript数组
- 数据间用逗号分隔
- 花括号用于保存对象
- 方括号用于保存数组
- JSON数据类型与Python的对照
Python |
JSON |
---|---|
dict | object |
list, tuple | array |
str, unicode | string |
int, long, float | number |
True | true |
False | false |
None | null |
Python中的JSON
- 将Python对象编码成json字符串 dumps()
- 他会自动调整格式
import json
data = {'name':'eric','id':1001}
print(json.dumps(data))
{"name": "eric", "id": 1001}
- 将json字符串解码Python对像 loads()
- 字典
import json
data = {'name':'eric','id':1001}
a = json.dumps(data)
print(json.loads(a))
{'name': 'eric', 'id': 1001}
- 元组和列表
- 都会转换成列表或者array的形式
import json
data1 = ("eric","is","old","involation","dog")
data2 = ["eric","is","old","involation","dog"]
j1 = json.dumps(data1)
j2 = json.dumps(data2)
print(j1,end=" this is j1\n")
print(j2,end=" this is j2\n")
a1 = json.loads(j1)
a2 = json.loads(j2)
print(a1,end=" this is a1\n")
print(a2,end=" this is a2\n")
###
["eric", "is", "old", "involation", "dog"] this is j1
["eric", "is", "old", "involation", "dog"] this is j2
['eric', 'is', 'old', 'involation', 'dog'] this is a1
['eric', 'is', 'old', 'involation', 'dog'] this is a2
- 将Python序列转换为json对象后写入文件 dump()
import json
data = {
'name':'eric',
'F1':["is","old","involation"],
'F2':("dog","hahaha","just joke")}
with open('json_test.txt','w+') as f:
json.dump(data,f)
##文件中的内容
{"name": "eric", "F1": ["is", "old", "involation"], "F2": ["dog", "hahaha", "just joke"]}
- 读取文件中的json文件字符串元素转换为Python类型 load()
import json
data = {
'id':'202101',
'a':["足","力","健"],
'b':("老","人","鞋")
}
with open('json_test.txt','w+') as f:
json.dump(data,f)
with open('json_test.txt','r+') as f:
print(json.load(f))
{'id': '202101', 'a': ['足', '力', '健'], 'b': ['老', '人', '鞋']}
- python的unicode字符串转换为json的字符串
>>> json.dumps(u"a")
'"a"'
- 查看json的类型
>>> type(json.dumps("abc"))
<class 'str'>
- dumps参数详解
dumps(obj,skipkeys=False,
ensure_ascii=True,
check_circular=True,
allow_nan=True,
separators=None,
default=None,
sort_keys=False, **kw):
skipkeys: | 如果为True的话,则只能是字典对象 |
ensure_ascii | 确定是否为ASCII编码 |
check_circular | 为True 循环类型检查 |
allow_nan | 是否为允许空值 |
separators | 对象分隔符 |
sort_keys | True的话,对于字典类型会按照键的ASCII码来排序 |
- dump参数详解
- 增加了一个文件对象,保存信息,很容易理解
- JSON到对象
import json
from faker import Faker
A = Faker(locale="zh_CN")
class Employee():
def __init__(self,name,tel):
self.name=name
self.tel=tel
B = Employee(A.name(),A.phone_number())
def jsonToClass(B):
return Employee(B['name'],B['tel'])
json_str = '{"name": "Harry Potter","tel":8888888888}'
B = json.loads(json_str, object_hook=jsonToClass)
print(B)
print(B.name)
CSV格式
- Comma-Separated Values CSV的含义
内置库csv按行读取
import csv
data = []
with open("a.txt","r") as file:
lines = csv.reader(file)
for line in lines:
data.append(line)
print(data)
[['1', '2', '3', '4', '5', '6', '7', '8', '9'], ['11', '12', '13', '14', '15', '16', '17', '18', '19'], ['11', '12', '13', '14', '15', '16', '17', '18', '19'], ['11', '12', '13', '14', '15', '16', '17', '18', '19'], ['11', '12', '13', '14', '15', '16', '17', '18', '19'], ['1', '2', '3', '4', '5', '6', '7', '8', '9'], ['1', '2', '3', '4', '5', '6', '7', '8', '9'], ['1', '2', '3', '4', '5', '6', '7', '8', '9']]
pandas读取
import pandas as pd
data = pd.read_csv("a.txt",header = None)
print(data)
print("!!!###!!!")
data = pd.read_csv("a.txt")
print(data)
0 1 2 3 4 5 6 7 8
0 1 2 3 4 5 6 7 8 9
1 11 12 13 14 15 16 17 18 19
2 11 12 13 14 15 16 17 18 19
3 11 12 13 14 15 16 17 18 19
4 11 12 13 14 15 16 17 18 19
5 1 2 3 4 5 6 7 8 9
6 1 2 3 4 5 6 7 8 9
7 1 2 3 4 5 6 7 8 9
!!!###!!!
1 2 3 4 5 6 7 8 9
0 11 12 13 14 15 16 17 18 19
1 11 12 13 14 15 16 17 18 19
2 11 12 13 14 15 16 17 18 19
3 11 12 13 14 15 16 17 18 19
4 1 2 3 4 5 6 7 8 9
5 1 2 3 4 5 6 7 8 9
6 1 2 3 4 5 6 7 8 9
numpy包读取
from numpy import genfromtxt
filename = "a"
filetype = ".txt"
#delimiter 分隔符
data = genfromtxt(filename+filetype, delimiter=',')
print(data)
[[ 1. 2. 3. 4. 5. 6. 7. 8. 9.]
[11. 12. 13. 14. 15. 16. 17. 18. 19.]
[11. 12. 13. 14. 15. 16. 17. 18. 19.]
[11. 12. 13. 14. 15. 16. 17. 18. 19.]
[11. 12. 13. 14. 15. 16. 17. 18. 19.]
[ 1. 2. 3. 4. 5. 6. 7. 8. 9.]
[ 1. 2. 3. 4. 5. 6. 7. 8. 9.]
[ 1. 2. 3. 4. 5. 6. 7. 8. 9.]]
dat文件
- from 百度:
- .dat并不是一种标准文件。许多文件都使用这个扩展名,但文件含义不同。而许多数据分析软件也用这个扩展名保存数据。所以这要看具体的软件情况来定。DAT文件,可以按照扩展名来看就是DATA的意思,即数据文件,这类文件并没有进行绝对化的定义
import numpy as np
filename = "a"
filetype = ".dat"
data = np.fromfile(filename+filetype)
print(data)
- 也许你很少用到dat文件
- 但是你需要知道,直接改个后缀是不能直接改变文件格式的,至少大多数情况下如此