python--file Operations

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.