python3 base notes (b): file operations, json operation

First, the file operations:

Basic operation:

f = open ( 'a.txt', encoding = 'utf-8') # default write is not read-only mode, there are characters to add utf-8 (r add prevent write file path translation, e.g. r'c : \ the User)
f.readlines () # put all the content files are read out, into the list which
f.readline () # read each line refers to,
f.read () # read all the contents
f.close () # Close file
f.write ( '123 \ n') # can only write string
f.writelines ([12,34]) # can be as long as the write cycle, the write list can
f.tell () # Get current pointer location
f.seek (0) # move the file pointer to a certain position, 0 for front
f.truncate () # empty the contents of the file
f . fulsh ( ) # after writing a file, immediately flush the buffer to write data to disk in
# Use with no need to open the file himself files 
with Open ( 'a.txt') AS F:
# plurality of intermediate open file, connecting
with open ( 'a.txt') as f, open ( 'a2 .txt ',' w ', encoding =' utf-8 ') as f2:

Several models file operations: 

name    mode Explanation
r read You can read, can not write, open a nonexistent file will complain
w write Can write, can not read, write time will clear the previous contents
a add to Can write, can not read, do not empty the contents of the previous write, if the file does not exist will create a new file
r+ Read-write mode You can read and write, open a nonexistent file will complain
w+ Read-write mode Can read and write, open the file will clear when the previous contents
a+ Additional reading mode Can read and write, the file pointer at the end of most, does not clear the previous file when you open the file,

 

 

 

 

 

 

When reading large files and efficient method:

Use read () and readlines () method of operating a file, then will be very card, we use a highly effective method, each read a line release a line:

Open = F ( 'file.txt', encoding = 'UTF-. 8') 
for in Line F:
Print ( 'the contents of each line:', line)

Modify the file:

There are two ways, the first is to directly modify, amend emptied after the original content, and then modify the content of the written document. The second is to open 2 files, modify the contents written to a new file

The first:

f=open('a.txt','a+',encoding='utf-8')
f.seek(0)
result=f.read()
new_result=result.replace('abc','ABC')
f.seek(0)
f.truncate() #清空文件内容
f.write(new_result)
f.close()

The second:

import os
f1=open('a.txt',encoding='utf-8')
f2=open('a2.txt','w',encoding='utf-8')
for line in f1: #循环a.txt的每一行
new_line=line.replace('1','一') #将1替换成一
f2.write(new_line)
f1.close()
f2.close()
os.remove('a.txt')
os.rename('a2.txt','a.txt')

Two, Json operation

json string is the string, the string can only be json Python in double quotes, only the contents written in the file string.

1, json.dumps () and json.loads content () can be converted to a file or database

= {D 'name': 'ABC'} 
Import JSON
json_str = json.dumps (D) the dictionary # / list converted to a string (JSON)
Print (json_str)

json_str2 = '{ "name": "ABC"}'
dic = json.loads (json_str2) # string (JSON) converted into a dictionary
print (dic)

2, only for json.dump file operations () and json.load ()

ensure_ascii = False characters may be displayed in json
# The json string into a string, a file written 
F = Open ( 'tata.json', 'W', encoding = 'UTF-. 8')
The json.dump (D, F, ensure_ascii = False, indent =. 4)

# json string read out of the dictionary to turn (load method for automatically read files)
F = Open ( 'tata.json', encoding = 'UTF-. 8')
DIC = the json.load (F)
Print (DIC)

 

 

 

Guess you like

Origin www.cnblogs.com/tata-learning/p/11575321.html