码迷,mamicode.com
首页 > 编程语言 > 详细

Python--文件操作

时间:2018-01-02 21:25:19      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:typeerror   rip   strip()   oop   big   line   反序   adl   文件   

print "**********文件操作***********"
data=[]   #列表
f=open("/home/soyo/桌面/spark编程测试数据/people.txt")
f2=open("/home/soyo/桌面/spark编程测试数据/peopele_out.txt","w")
lines=f.readlines()
print lines
for line in lines:
    line=line.strip()
    print line
    data.append(line)
    f2.write(line+","+"\n")
print data
for i in data:
    print i
f.close()
print "********使用Json处理数据*********"
import json
x1={"soyo":2,"Hadoop":77}
print x1
x=dict(height=23,weight=99)
print x
y=json.dumps(x)
print "序列化后的字典:",y         #序列化是字符串
x3=json.loads(y)
print "反序列化还原为原来的字典:",x3
print x3["height"] 
#使用dump和load -->字典保存为Json
f=open("/home/soyo/桌面/spark编程测试数据/bigData.json","w")
json.dump(x,f)
f.close()
f=open("/home/soyo/桌面/spark编程测试数据/bigData.json")
data=json.load(f)
print "Json 数据为:",data
f.close()
#  f.write 会报错
# f.write(x)
#TypeError: expected a string or other character buffer object
# f=open("/home/soyo/桌面/spark编程测试数据/bigData2.json","w")
# f.write(x)

结果:

**********文件操作***********
[‘soyo8,35\n‘, ‘\xe5\xb0\x8f\xe5\x91\xa8,30\n‘, ‘\xe5\xb0\x8f\xe5\x8d\x8e,19\n‘, ‘soyo,88\n‘]
soyo8,35
小周,30
小华,19
soyo,88
[‘soyo8,35‘, ‘\xe5\xb0\x8f\xe5\x91\xa8,30‘, ‘\xe5\xb0\x8f\xe5\x8d\x8e,19‘, ‘soyo,88‘]
soyo8,35
小周,30
小华,19
soyo,88
********使用Json处理数据*********
{‘soyo‘: 2, ‘Hadoop‘: 77}
{‘weight‘: 99, ‘height‘: 23}
序列化后的字典: {"weight": 99, "height": 23}
反序列化还原为原来的字典: {u‘weight‘: 99, u‘height‘: 23}
23
Json 数据为: {u‘weight‘: 99, u‘height‘: 23}

Python--文件操作

标签:typeerror   rip   strip()   oop   big   line   反序   adl   文件   

原文地址:https://www.cnblogs.com/soyo/p/8178858.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!