1. Wrong way to open
#coding=utf-8

f = open("test.txt",'w+')
f.write('Mars is slim,isn\'he? \n Martian religion ')
print f.read()
f.close()

Console results :


2. The right way to open
It is recommended to close the file after writing , Then open it again , Finally, read the file
If your txt The file code is utf-8, You need to in f.read() add to decode('utf-8'), namely f.read().decode('utf-8') that will do         
The code is as follows :
#coding=utf-8

# Write it like this , After reading and writing the file, open Notepad without garbled code
f = open("test.txt",'w+')
f.write('Mars is slim,isn\'he? \n Martian religion ')
f.close()

f = open("test.txt").decode('utf-8')
print f.read()
f.close()

Results show :

python There are more related articles summarized in the garbled document reading and writing

  1. python File reading and writing and form conversion and CGI Simple application of

    I. 1 python Read and write study notes open() Will return a file object , The basic syntax is as follows : open(filename, mode) filename: String value containing the name of the file you want to access . mo ...

  2. python File read and write operations (24)

    Previous code output data directly to the console , In fact, we can also read through / How to write a file read / Output to disk file , Short for reading and writing documents I/O operation . file I/O The operation is divided into four parts : open (open)/ Read (read)/ write in (wri ...

  3. Python File read and write (open(),close(),with open() as f...)

    Python Built in functions for reading and writing files , Usage and C Is compatible . This section is about : Opening of files / close . File object . Reading and writing of documents, etc . This chapter is just an example of TXT Reading and writing of type documents , That is, the most basic file reading and writing , We also need to pay attention to coding ...

  4. Python File read and write , condition loop ( Login three times to lock the account instance )

    Read and write through files , Conditional loop related grammar , If the login fails for three times, the account will be locked Need one """ Requirements describe : 1. Enter the correct account number , password , Exit procedure 2. Login failed , Re enter the account password 3. Same account ...

  5. Python Three modes and differences of file reading and writing

    #coding=utf-8 #__author:Administrator #__time:2018/5/9 13:14 #__file_name:text1 import io # The one that can call a method must be ...

  6. 【 Study 】python File read and write , use with open as The benefits of , very nice 【 Reprint 】

    Link to the original text :http://www.cnblogs.com/ymjyqsx/p/6554817.html remarks : Bloggers also have a lot of notes to learn , If you have problems, you can read , Thank you very much for your summary Reading and writing files is the most common IO operation ...

  7. python File read and write , Use it later with open sentence

    Reading and writing files is the most common IO operation .Python Built in functions for reading and writing files , Usage and C Is compatible . Before reading and writing the document , We must first understand , The function of reading and writing files on disk is provided by the operating system , The modern operating system does not allow ordinary programs to operate the disk directly , ...

  8. python How to read and write files

    One . Common file reading and writing methods 1. Read file information : with open('/path/to/file', 'r') as f: content = f.read() 2. Write to file : with open('/U ...

  9. Python File read and write 、StringIO and BytesIO

    1 IO The meaning of In the computer ,IO yes Input/Output Abbreviation , That's input and output . Because the program and runtime data reside in memory , from CPU This super fast computing core to execute , Where it comes to data exchange , Usually disk . Network, etc , Just ...

Random recommendation

  1. [LeetCode] Strobogrammatic Number Symmetric numbers

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  2. Mysql Operating instructions for

    Mysql The control of user's operation authority is in :mysql.user In the table User Field : Represents the user name : Host Field : Represents the address that the user is allowed to access , Can be domain name ( Such as localhost). Host name .ip and %:% There is no limit to ...

  3. C#WinForm The application program can automatically fill in the user name and password on the web page and click the login button 【 Reprint 】

    Use WebBrowser Control , stay documentComplete The event handler says HtmlElement name = webBrowser1.Document.GetElementById(" ...

  4. Java The dynamics of programming The first 1 part : Classes and class loading -- Reprint

    Original address :http://www.ibm.com/developerworks/cn/java/j-dyn0429/ This is the first article in this new series , This series will discuss what I call  Java The dynamic nature of programming ...

  5. MySQL Introduction to database operation

    start-up mysqld: Start... On the command line mysql when , If not "--console", start-up . The closing information is not displayed in the interface , It's recorded in the installation directory data Directory , The file name is usually hostname.err, ...

  6. poj3258 Two points The minimum maximization problem

    River Hopscotch Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10842   Accepted: 4654 ...

  7. PHP in HTTP_HOST and SERVER_NAME The difference between

    Recently in the development of station group software , Used according to the domain name to determine the sub site related issues .PHP There are two variables to get the current domain name HTTP_HOST and SERVER_NAME, I want to know the difference and which is more reliable . First of all, I want to say , What's on Baidu ...

  8. Linux(CentOS6.5) Download and install Nginx The latest official stable version (nginx-1.10.0)

    notes : This article has been updated to a new version :http://comexchan.cnblogs.com/p/5815753.html , Please check the new version directly , thank you ! This paper addresses http://comexchan.cnblogs.co ...

  9. python_16_ serialize

    How to realize the interaction between different programming languages ? json data , It's a bridge between languages What is? json data ? imoprt json json.dumps( Content )                    -- Transform the content ...

  10. Python And Django rest_Framework Add

    One . What is? RESTful REST It's not about technology , Represents a software architecture style ,REST yes Representational State Transfer For short , Chinese translation for " Characterization state transfer " ...