基本命令
pd.read_csv('1.txt')
只想读几行文本文件
pd.read_csv('1.txt', nrow=5)
要逐块读取文件,需要设置chunksize(行数)
chunker=pd.read_csv('1.txt',chunksize=1000)
写出到文本文件(以逗号分隔的文件)
data.to_csv('1.txt')
当然可以使用其他分隔符
data.to_csv('1.txt', sep='|')
JSON数据
使用json.loads将JSON字符串转化为Python形式
obj='''
{
"name":"Wes",
"place_lived":["USA","Spain","Germany"],
"pet":null,
"siblings":[{"name":"Young","age":25,"pet":"Zuko"},
{"name":"Yo","age":24,"pet":"Zuk"}]
}
'''
import json
result=json.loads(obj)
result
{'name': 'Wes',
'pet': None,
'place_lived': ['USA', 'Spain', 'Germany'],
'siblings': [{'age': 25, 'name': 'Young', 'pet': 'Zuko'},
{'age': 24, 'name': 'Yo', 'pet': 'Zuk'}]}
type(result)
dict
json.dumps则将Python对象转化成JSON对象
asjson=json.dumps(result)
asjson
'{"name": "Wes", "siblings": [{"name": "Young", "pet": "Zuko", "age": 25}, {"name": "Yo", "pet": "Zuk", "age": 24}], "place_lived": ["USA", "Spain", "Germany"], "pet": null}'
type(asjson)
str
将JSON对象放入DataFrame
siblings=DataFrame(result['siblings'])
siblings
age name pet
0 25 Young Zuko
1 24 Yo Zuk