Python加载Laravel 8的PHP文件时出现UnicodeDecodeError求助
UnicodeDecodeError解决:读取Laravel项目PHP文件编码问题
我写了一个Python程序用来生成类图,加载Laravel 8项目的PHP文件时,即便指定了utf-8编码,还是触发了UnicodeDecodeError。相关代码和报错信息如下:
代码
import os import re import tempfile import webbrowser # Define the path to the Laravel project project_path = 'C:/xampp/htdocs/Laravel-8-project' # Find all the PHP class files in the project def find_class_files(dir): results = [] for file in os.listdir(dir): file_path = os.path.join(dir, file) if os.path.isdir(file_path): results.extend(find_class_files(file_path)) elif os.path.splitext(file)[1] == '.php': results.append(file_path) return results # Parse the class names and dependencies from the class files def parse_classes(class_files): classes = [] for file in class_files: with open(file, 'r', encoding='utf-8') as f: contents = f.read() class_name = get_class_name(contents) if class_name: dependencies = get_dependencies(contents) classes.append({ 'name': class_name, 'dependencies': dependencies, }) return classes # Extract the class name from a PHP class file # Extract the dependencies from a PHP class file # Generate an HTML class diagram from a list of classes # Display an HTML class diagram in the browser # Find all the PHP class files in the Laravel project class_files = find_class_files(project_path) # Parse the class names and dependencies from the class files classes = parse_classes(class_files) # Generate the HTML for the class diagram html = generate_class_diagram(classes) # Display the class diagram in the browser display_class_diagram(html)
报错信息
Traceback (most recent call last): File "C:\Users\inaqi\Desktop\cd.py", line 77, in <module> classes = parse_classes(class_files) File "C:\Users\inaqi\Desktop\cd.py", line 25, in parse_classes contents = f.read() File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\codecs.py", line 322, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 105: invalid continuation byte
解决方法
1. 尝试其他常见编码
Laravel项目里的PHP文件可能使用非UTF-8编码,比如ISO-8859-1、GBK这类。0xe9字符在ISO-8859-1中对应é,是合法字符,你可以先替换编码参数试试:
# 试试ISO-8859-1 with open(file, 'r', encoding='ISO-8859-1') as f: # 或者试试GBK with open(file, 'r', encoding='gbk') as f:
2. 自动检测文件编码
用chardet库自动识别文件编码,先安装依赖:
pip install chardet
然后修改parse_classes函数:
import chardet def parse_classes(class_files): classes = [] for file in class_files: # 先以二进制读入文件 with open(file, 'rb') as f: raw_data = f.read() # 检测编码类型 detect_result = chardet.detect(raw_data) file_encoding = detect_result['encoding'] # 用检测到的编码解码内容 contents = raw_data.decode(file_encoding) class_name = get_class_name(contents) if class_name: dependencies = get_dependencies(contents) classes.append({ 'name': class_name, 'dependencies': dependencies, }) return classes
3. 跳过/替换错误字符
如果不需要保留特殊字符,可以在读取时忽略解码错误,或者用占位符替换:
# 忽略错误字符 with open(file, 'r', encoding='utf-8', errors='ignore') as f: # 用�替换错误字符 with open(file, 'r', encoding='utf-8', errors='replace') as f:
内容的提问来源于stack exchange,提问作者Naqi Ch
相关产品推荐
相关产品推荐

