How to deal with python file reading failure

When reading files, such as reading xxx.csv, encoding errors may be reported

Similar to

‘xxx’ codec can’t decode byte 0xac in position 211: illegal multibyte sequen

id_list =[]withopen('E:/work_spider/xxx/xx.csv',"r", encoding="utf-8")as csvfile:
 csvReader = csv.reader(csvfile)for content in csvReader:
 content =str(content)if'l.'in content:continue
 id_list.append(content.split('\')[0].replace("['", ""))

You can try to develop an encoding method when reading.

Unicode decode error xxxxxxxxxxx may also be reported when saving pictures or video files

VideoHtmlContent = requests.get(url = VideoUrl,headers=headers).content
withopen('bobovideo.mp4','wb',)as f:
 f.write(VideoHtmlContent)

Don't forget its file opening method, pictures and videos are all requested and written in bytes type binary mode. We use'wb' to open in binary writing mode

There are many open modes for open. The following are for reference and reference only:

a means append, r means read, w means write, and + means read and write mode. , B means binary, t means text mode, t is the default mode.

w is opened for writing,
a Open in append mode (start from EOF, create a new file if necessary)
r+ open in read-write mode
w+ open in read-write mode
a+ Open in read-write mode
rb opens in binary read mode
wb opens in binary writing mode
ab opens in binary append mode
rb+ opens in binary read-write mode
wb+ opens in binary read-write mode
ab+ opens in binary read-write mode

Question extension:

Python file reading: errors encountered and solutions

TypeError: ‘str’ object is not callable

cause:

The error TypeError:'str' object is not callable literally means: that str cannot be called by the system,

In fact, the reason is: you are calling a variable or object that cannot be called. The specific manifestation is that you call the function or variable in the wrong way.

example:

filePath=kwargs['path']
filePathStr=str(filePath)

That is, I am using keyword parameters to pass parameters, what type is passed when it is passed, what type is passed, that is, filePath is originally a string type, but I used the str() function to act on it. This is the problem, the function call is wrong!

So far, this article on how to deal with python file reading failures is introduced. For more related python file reading failures, please search for the previous articles of ZaLou.Cn or continue to browse the related articles below. Hope you will get more Support ZaLou.Cn!

Recommended Posts

How to deal with python file reading failure
How to make a globe with Python
How to process excel table with python
How to get started quickly with Python
How to convert web pages to PDF with Python
How to play happily with Python3 on Ubuntu
How to switch the hosts file using python
Python how to delete rows with empty columns
How to read and write files with Python
How to practice after the python file is written
How to comment python code
How to learn python quickly
How to uninstall python plugin
How to understand python objects
How to use python tuples
python how to view webpage code
How to use python thread pool
How to wrap in python code
How to save the python program
How to omit parentheses in Python
How to install Python 3.8 on CentOS 8
How to install Python 3.8 on Ubuntu 18.04
How to write classes in python
How to filter numbers in python
How to read Excel in Python
How to install Python on CentOS 8
How to solve python dict garbled
How to write return in python
How Python operates on file directories
How to view the python module
How to clear variables in python
How to understand python object-oriented programming
How to use SQLite in Python
A practical guide to Python file handling
How to verify successful installation of python
How to use and and or in Python
How to introduce third-party modules in Python
How to represent null values in python
How to use PYTHON to crawl news articles
How to write win programs in python
How to install third-party modules in Python
How to custom catch errors in python
How to write try statement in python
How to define private attributes in Python
R&D: How To Install Python 3 on CentOS 7
How to understand global variables in Python
How to view installed modules in python
How to install Python2 on Ubuntu20.04 ubuntu/focal64
How to debug python program using repr
How to learn the Python time module
How to open python in different systems
How to enter python triple quotation marks
How to add background music in python
How to represent relative path in python
How to monitor CentOS 7 server with Prometheus
How to install Prometheus with Docker on Ubuntu 14.04
How to upgrade all Python libraries on Ubuntu 18.04
How to use the round function in python
How to program based on interfaces in Python
How to install python in ubuntu server environment
How to manage Jenkins with Rancher on Ubuntu 14.04