Python Basics 5-File | JSON serialization

Source: Internet
Author: User
Tags readline serialization

The following code is not tested, just as a code snippet as a future reference, these dead APIs are put in here for a moment to look at the summary

ImportSYSPrint(Sys.path)#---------------file----------------------------#The first kind of direct wayfile1 = open ("Test.txt") File2= Open ("output.txt","W")#W means write (overwrite) r means read a represents append (write -up) whileTrue:line=file1.readline () file2.write ('"'+ Line[:s] +'"'+",")    if  notLine : Breakfile1.close () file2.close ( )#read () reads all lines of a text file into a string#ReadLine () reads a row#readlines () reads all lines of text into a list, and each row is an element of the list#The second type of file iteratorFile2 = open ("output.txt","W") forLineinchOpen"Test.txt"): File2.write ('"'+ Line[:s] +'"'+",")#third type of file context manager#Open FileWith open ("Somefile.txt","R") as F:data=F.read ()#Loop the entire documentWith open ("Somefile.txt","W") as F: forLineinchF:#process each row#writing textWith open ("Somefile.txt","W") as F:f.write ("XXX") F.write ("XXX")#to write the printed line to the fileWith open ("Somefile.txt","W") as F:Print(Line1, file=f)Print(Line2, file=f)#binary file read/writef = open ("edc.jpg","RB")Print(F.read ())#output \xff\xd8 .... Hexadecimal representation of bytes#any non-standard text file (Py2 standard is ASCII, PY3 is Unicode), read the file in binary, use. Decode () to decodef = open ("DeGuangGuo.txt","RB") U= F.read (). Decode ('Deyuncode')#operation of files and directories#Python calls the built-in OS module to invoke the operating system's interface functionsImportOsos.name#POSIX = = Nix NT = = WindowsOs.uname ()#View specific Information#environment variable exists in Os.environ is List#absolute path to the current directoryOs.path.abspath ('.')#Create a new directory under a directory to represent the new directoryOs.path.join ('/users/edc','Pictures')#get a string that is a new path#Create a directoryOs.mkdir ('/users/edc/pictures/')#Delete directoryOs.rmdir ('/users/edc/pictures')#splitting a stringOs.path.split ('/users/edc/pictures/aj.avi')#split into two parts, followed by the last level of the directory or file#('/users/edc/pictures/', ' Aj.avi ')#get file name extensionOs.path.splitext ('/users/edc/pictures/aj.avi')#('/users/edc/pictures/aj ', '. avi ')#file RenameOs.rename ('xxx.xx','BBB')#Deleting FilesOs.remove ('XXX')#you can use Shutil to help us with our files.#List all directories in the current directory[x forXinchOs.listdir ('.')ifOs.path.isDir (x)]#list. py Files[x forXinchOs.listdir ('.')ifOs.path.isDir (x) andOs.path.splitext (x) [1] = ='. PY']#serialization from memory to hard disk or transfer process for serialization from hard disk to memory for deserializationImportPickled= Dict (name='Jack', age=23, score=60) Str= Pickle.dumps (d)#call Pickle's dumps function for serialization processingPrint(str) F= Open ("Dump.txt","WB") Pickle.dump (d, F)#serialization of content to a filef.close ()#deserializationImportPicklef= Open ("Dump.txt","RB") d= Pickle.load (f)#call load to do deserializationf.close ()Print(d)Print('name is%s'% d['name'])#Python2 and 3 inside of the pickle inconsistent, in order to ensure HarmonyTry:    ImportCpickle as PickleexceptImporterror:ImportPickle#JSON serialization uses this library of JSON toImportJsond1= Dict (name='Jack', age = score=32,) Str= Json.dump (D1)#Serialization ofD2= Json.loads (str)#deserialization

Python Basics 5-File | JSON serialization

Related Article

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.