python advanced: file reading and writing

Read and write txt files

read file

  1. Open a file object in the mode of reading a file, use Python's built-in open() function, and pass in the file name and identifier:
>>> f = open('/Users/michael/test.txt', 'r')
  1. Call the read() method to read all the contents of the file at one time. Python reads the contents into memory, represented by a str object:
>>> f.read()
'Hello, world!'
  1. Call the close() method to close the file. The file must be closed after use, because the file object will occupy the resources of the operating system, and the operating system can open a limited number of files at the same time:
>>> f.close()
  1. Python introduced the with statement to automatically call the close() method for us:
    It has the same function as try... Finally, but the code is more concise, and there is no need to call the f.close() method.
with open('/path/to/file', 'r') as f:
    print(f.read())
  1. If the file is small, read() is the most convenient for one-time reading; If the file size cannot be determined, it is safer to call read(size) repeatedly; If it is a configuration file, it is most convenient to call readlines():
for line in f.readlines():
    print(line.strip()) # Delete the '\n' at the end

Read binary file

>>> f = open('/Users/michael/test.jpg', 'rb')
>>> f.read()
b'\xff\xd8\xff\xe1\x00\x18Exif\x00\x00...' # Hexadecimal bytes

Read non UTF-8 files

To read non UTF-8 encoded text files, you need to pass the encoding parameter to the open() function. For example, to read GBK encoded files:

>>> f = open('/Users/michael/gbk.txt', 'r', encoding='gbk')
>>> f.read()
'test'

When encountering some files with nonstandard encoding, you may encounter UnicodeDecodeError, because some illegal encoded characters may be mixed in the text file. In this case, the open() function also receives an errors parameter, indicating how to deal with coding errors. The simplest way is to ignore directly:

>>> f = open('/Users/michael/gbk.txt', 'r', encoding='gbk', errors='ignore')

Write file

Write file in'w'mode
Write in'a'append mode

>>> f = open('/Users/michael/test.txt', 'w')
>>> f.write('Hello, world!')
>>> f.close()

You can call write() repeatedly to write the file, but be sure to call F Close() to close the file. When we write a file, the operating system often does not write the data to the disk immediately, but puts it into the memory cache and writes it slowly when we are free. Only when the close () method is called can the operating system guarantee to write all the data not written to the disk. The consequence of forgetting to call close() is that only part of the data may be written to the disk, and the rest may be lost. Therefore, use the with statement to be safe:

with open('/Users/michael/test.txt', 'w') as f:
    f.write('Hello, world!')

Read all contents of txt file and return in list form

The contents of the document are as follows

entry name,Version number
 Conductor structure inspection,V1.0
 Conductor structure and monofilament diameter inspection,V1.0

It is hoped to change to the following form [[conductor structure inspection, V1.0], [conductor structure and monofilament diameter inspection, V1.0]]
The code is as follows

line_list=[]
file_list=[]


def read_list():
    f = open(path, "r")
    lines = f.readlines()
    for line in lines:
        line_list = line.strip().split(',')
        file_list.append(line_list)

    return file_list

Reading and writing EXCEl files

  • xlrd: read EXCEL
  • xlwt: write EXCEL
    be careful
    xlrd and xlwt deal with xls files. The maximum number of lines in a single sheet is 65535. If there is a greater need, it is recommended to use openpyxl function, and the maximum number of lines reaches 1048576.
    If the data volume exceeds 65535, you will encounter: valueerror: row index was 65536, not allowed by.Xls format

Basic usage of reading excel xlrd

  1. Open excel
import xlrd

workbook = xlrd.open_workbook('./test.xlsx')
  1. Get sheet of excel file
# Method 1: use sheet name
sheet = workbook.sheet_by_name('Sheet1') 
# Method 2: index
sheet = workbook.sheet_by_index(0)#Get sheet s according to index order
  1. Get the maximum number of rows and columns of the sheet
rows = sheet_name.nrows # Get the number of rows
cols = sheet_name.ncols # Get the number of columns
  1. Get the data of sheet
# Method 1: get all the data of the specified row or column
sheet.col_values(0) # Get all the data in the first column
sheet.row_values(0) # Get all the data in the first row
# Method 2: get the specified cell data
sheet.cell_value(0, 1) #Get the data of column 1 and row 2

Basic usage of writing excel xlwt

  1. Open an excel
import xlwt
book = xlwt.Workbook()#Create a new excel

  1. Add a sheet
sheet = book.add_sheet('case1_sheet')#Add a sheet page

  1. Write values in sheet
 sheet.write(row,col,value)# Write values in a row or column

  1. Save excel
    Note: be sure to save
book.save('test.xls')#Save to current directory

Reading and writing JSON files

JSON (JavaScript Object Notation) is a lightweight data exchange format. It is based on a subset of ECMAScript.
python operates json files in two ways:

  1. load(): used to read json files
  2. dump(): used to write json

Read JSON file

# Read JSON file, file content: [{'a':'b'}]
import json

with open('./2.json', 'r', encoding='utf-8')as fp:
    json_data = json.load(fp)
    print("File data:", json_data)
    print(type(json_data))

File data: [{'a':'b'}]
<class 'list'>

Write dictionary data to JSON file

# Write dictionary data to JSON file
dict1 = {"name": "sxy", "sex": "male"}
with open('./1.josn', 'a', encoding='utf-8')as fp:
    json.dump(dict1, fp, ensure_ascii=False)  # ensure_ascii=False, indicating that the return value can contain non ascii values

Tags: Python

Posted by J4B on Mon, 08 Aug 2022 02:40:27 +0930