Getting Started with Python: Files

 

 

 

 

 

 

python file processing

 

  1. File operations are divided into read, write, and modify

1.1 File reading

Example 1:

f = open(file='user_list', mode='r', encoding='utf-8')
data = f.read()
f.close()
print(data)
  • file='user_list' #represents the file path
  • mode='r' # means read only
  • encoding='utf-8' #Indicates that 0101010 on the hard disk is "segmented" according to the rules of utf-8, and then each segment of 0101010 after "sentence segmentation" is converted into Unicode 01010101. There are 01010101 and characters in the Unicode comparison table. relation
  • f.read() means to read all the content, the content is the converted string
  • f.close() means to close the file

 

Example 2:

f = open(file='user_list', mode='rb')
data = f.read()
f.close()
print(data)

  

  • file='user_list' #represents the file path
  • mode='rb' # means read only
  • f.read() means to read all the content, the content is 010101010 originally saved in a certain encoding on the hard disk, that is: the byte type of a certain encoding format
  • f.close() means to close the file

 

What is the difference between example 2 and example 1?

    In example 2, the encoding is not specified when opening the file, just why? It is because the file is opened directly in rb mode. rb refers to binary mode. The data is read directly into the memory in bytes format. If you want to see the content, you need to manually decode it. Therefore, you do not need to specify the encoding during the file opening stage.

 

Q: What if I don't know the encoding of the file to be processed?

 

import chardet
f = open(file='user_list', mode='rb')
data = f.read()
f.close()

result = chardet.detect(open('user_list', mode='rb').read())

print(result)

 

  

loop file

 

 1.2 Writing files

 

f = open(file='user_list1', mode='w', encoding='utf-8')
f.write('lala Du,26,13898424612,Operation,2017-07-02')
f.close()

 

  The above operation syntax explanation:

  • file='user_list1' # Indicates the file path
  • mode = 'w' # means write only
  • encoding='utf-8' # Encode the Unicode string to be written into utf-8 format
  • f.write(....) # Indicates the written content. The written content is a Unicode string type, which will be internally converted to the specified encoding 01101010101 according to the encoding, that is: the byte type

Write in binary mode: wb

 

1.3 Add

Append the content to the end of the file

 

f = open(file='user_list1', mode='a', encoding='gbk')
f.write('\nTom,28,13898424613,Operation,2017-07-02')
f.close()

 

  

Notice:

  • When the file is opened in "a" or "ab" mode, it can only be appended, that is: append content to the end of the original content
  • When writing to the hard disk, it must be 0101010 in some kind of encoding, and you need to pay attention when opening:

                ab, when writing, you need to directly pass in 0100101 in a certain encoding, that is: byte type

                 a and encoding, you need to pass in a Unicode string when writing, and internally convert the Unicode string to the encoded 010101010 according to the encoding specified by encoding

 

1.4 Read and write mixed mode

f = open(file='user_list1', mode='r+', encoding='gbk')
data = f.read()     # 可以读内容
print(data)
f.write('\nJacke,30,13898424616,Operation,2017-07-02')   # 可以写
f.close()

  结果:

 

 1.5 修改文件

 尝试直接以r+模式打开文件,默认会把新增的内容追加到文件最后面,想要修改中间的内容,怎么办?

可以用seek(中间位置)

f = open(file='user_list1', mode='r+', encoding='utf-8')
f.seek(6)
f.write("测试工程师")
f.close()

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324728917&siteId=291194637