I am trying to read all PDF files from a folder to look for a number using regular expression. On inspection, the charset for PDFs is 'UTF-8'.
Throws this error:
'utf-8' codec can't decode byte 0xe2 in position 10: invalid continuation byte
Tried reading in binary mode, tried Latin-1 encoding, but it shows all special characters so nothing shows up in search.
import os
import re
import pandas as pd
download_file_path = "C:\\Users\\...\\..\\"
for file_name in os.listdir(download_file_path):
try:
with open(download_file_path + file_name, 'r',encoding="UTF-8") as f:
s = f.read()
re_api = re.compile("API No\.\:\n(.*)")
api = re_api.search(s).group(1).split('"')[0].strip()
print(api)
except Exception as e:
print(e)
Expecting to find API number from PDF files