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+","+" ") 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
', 'xe5xb0x8fxe5x91xa8,30
', 'xe5xb0x8fxe5x8dx8e,19
', 'soyo,88
']
soyo8,35
小周,30
小华,19
soyo,88
['soyo8,35', 'xe5xb0x8fxe5x91xa8,30', 'xe5xb0x8fxe5x8dx8e,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}