UTF-16 流不以 BOM 开头

问题描述 投票:0回答:0

循环遍历 .txt 文件以使用文件名和文件文本填充数据框: 对于 i, f in enumerate(files):

# Open the file for reading
file = open(f, encoding='utf-16') # Changed from default encoding of utf-8
    
# Read the file contents
text = file.read()

# Assign the text from the file to index 'i' at column 'text'
# using the .at accessor. Note that this modifies the dataframe
# "in place" so you don't need to assign the result into a variable.
df.at[i, 'text'] = text

# Convert the filenames list created above into a new 'filename' column
df['filename'] = pd.DataFrame({'col':files})

u-16-be 它没有用

python error-handling unicode decoding
© www.soinside.com 2019 - 2024. All rights reserved.