Print "********** file Operation ***********"Data=[]#ListF=open ("/home/soyo/Desktop/spark Programming test data/people.txt") F2=open ("/home/soyo/Desktop/spark Programming test data/peopele_out.txt","W") Lines=F.readlines ()PrintLines forLineinchLines:line=Line.strip ()PrintLine Data.append-f2.write ( line+","+"\ n")PrintData forIinchData:Printif.close ()Print "******** using JSON to process data *********"Importjsonx1={"Soyo": 2,"Hadoop": 77}Printx1x=dict (height=23,weight=99)PrintXY=json.dumps (x)Print "Serialized Dictionary:"Y#serialization is a stringx3=json.loads (y)Print "deserialization reverts to the original dictionary:", X3Printx3["Height"] #Save as JSON using dump and load--dictionaryF=open ("/home/soyo/Desktop/spark Programming test data/bigdata.json","W") Json.dump (x,f) f.close () F=open ("/home/soyo/Desktop/spark Programming test data/bigdata.json") Data=json.load (f)Print "The Json data is:", Dataf.close ()#F.write will make an error .#f.write (x)#typeerror:expected A string or other character buffer object#f=open ("/home/soyo/Desktop/spark Programming test Data/bigdata2.json", "W")#f.write (x)
Results:
File Operation ***********
[' soyo8,35\n ', ' \xe5\xb0\x8f\xe5\x91\xa8,30\n ', ' \xe5\xb0\x8f\xe5\x8d\x8e,19\n ', ' soyo,88\n ']
Soyo8,35
Small week, 30
Xiao Hua, 19
soyo,88
[' soyo8,35 ', ' \xe5\xb0\x8f\xe5\x91\xa8,30 ', ' \xe5\xb0\x8f\xe5\x8d\x8e,19 ', ' soyo,88 ']
Soyo8,35
Small week, 30
Xiao Hua, 19
soyo,88
Using JSON to process data *********
{' Soyo ': 2, ' Hadoop ': 77}
{' Weight ':, ' height ': 23}
Serialized dictionary: {"Weight":, "height": 23}
Deserialization reverts to the original dictionary: {u ' weight ': $, U ' height ': 23}
23
Json data is: {u ' weight ': £ º, u ' height ': 23}
python--file Operations