Read a file containing garbled lines in Python

In the log file of the Web application that has not been tested in detail, some lines may be garbled (such as the error message output by the application).

If you try to read the log file with Python while trying to get statistical information from such a log file, as expected, an exception "Unable to decode" will occur on the garbled line.

Also, when suddenly an unknown system log is passed and an analysis is requested, an error "Unable to decode" may occur when reading with Pyhon even though it looks like a normal line at first glance. In this case, you can carefully investigate the character code of the log file, but if the line is not related to analysis, you may not have time to deal with the error that occurs.

Therefore, when opening the file, if the parameter "` ʻerrors ='replace'``" is specified in the argument, the garbled part (the part where decoding failed) is replaced with "?" And read. Will give you.

f = open('foo.log', mode='r', errors='replace')

I want to aggregate statistical information from the log, but please try it when you do not have time to write the processing of the decoding error.

Have a good Python life! !!

Recommended Posts

Read a file containing garbled lines in Python
Create a binary file in Python
Read the file line by line in Python
Read the file line by line in Python
[GPS] Create a kml file in Python
[Python] Read the specified line in the file
Read a file in Python with a relative path from the program
File operations in Python
How to read a CSV file with Python 2/3
Read DXF in python
Create a GIF file using Pillow in Python
How to create a JSON file in Python
Read table data in PDF file with Python
Read a Python # .txt file for a super beginner in Python with a working .py
File operations in Python
Extract lines containing a specific "string" in Pandas
Create a package containing global commands in Python
Create a MIDI file in Python using pretty_midi
Read line by line from a file with Python
Read Python csv file
How to read a file in a different directory
Various ways to read the last line of a csv file in Python
Parse a JSON string written to a file in Python
A memorandum to run a python script in a bat file
I want to randomly sample a file in Python
Run a Python file with relative import in PyCharm
How to read csv containing only integers in Python
Take a screenshot in Python
[Python 3] Prime factorization in 14 lines
Create a function in Python
Download the file in Python
Read Euler's formula in Python
Read and write a file
Read Namespace-specified XML in Python
Read Outlook emails in Python
Make a bookmarklet in Python
Make python segfault in 2 lines
Write and read a file
Draw a heart in Python
Python install in 2 lines @Windows
Read Fortran output in python
Try creating a Deep Zoom file format .DZI in Python
Change the standard output destination to a file in Python
How to import a file anywhere you like in Python
[Note] Import of a file in the parent directory in Python
Python> Read from a multi-line string instead of a file> io.StringIO ()
Maybe in a python (original title: Maybe in Python)
Count specific strings in a file
Write a binary search in Python
File / folder path manipulation in Python
[python] Manage functions in a list
Hit a command in Python (Windows)
Read Protocol Buffers data in Python3
Save the binary file in Python
Create a DI Container in Python
Linebot creation & file sharing in Python
Draw a scatterplot matrix in python
ABC166 in Python A ~ C problem
Write A * (A-star) algorithm in Python
Read PNG chunks in Python (class)
Solve ABC036 A ~ C in Python