# 读取txt编码格式
import chardet
cc =input()
with open(cc,'rb') as f:
print(chardet.detect(f.read()))
# 报错原因
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
start(fakepyfile,mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(), __main__.__dict__) File "<string>", line 2, in <module>
ModuleNotFoundError: No module named 'chardet'
[程序已完成]
# 这是我失败后得到的另一种读取方法
cc =input()
def get_encoding(cc):
"""以二进制方式读取文件,获取字节数据,检测编码格式"""
with open(cc, 'rb') as f:
return chardet.detect(f.read())['encoding']
def file_read(cc, file_encoding):
"""以检测到的文件编码格式来读取文件"""
with open(cc, 'r', encoding=file_encoding) as f:
file_content = f.read()
return file_content
搞得人月经不调!