UnicodeEncodeError:“charmap”编解码器无法对位置910中的字符进行编码:字符映射到<undefined>

2024-06-09 06:10:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试读取.mat文件并以.txt格式保存所有数组。我一直试图写在.txt文件,但后来我得到了这个错误

UnicodeEncodeError: 'charmap' codec can't encode characters in position 9-10: character maps to <undefined>

我试着找出原因,并且知道在这个.mat文件中,我有一个类似这样的数组

array([array(['丰田Avalon'], dtype='<U8')], dtype=object)

我很确定这个错误就是因为这个。但找不到如何将其转换为.txt格式以消除此错误。我的密码是

import os
import scipy.io as cio

mat = cio.loadmat("D:/compCarsThesisData/data/misc/make_model_name.mat")
model_names = mat['model_names']

path = "D:/compCarsThesisData/data/image/"
count = 0
for root, _, files in os.walk(path):
  cdp = os.path.abspath(root)
  for f in files:
    name,ext = os.path.splitext(f)
    if(ext==".jpg"):
      cip = os.path.join(cdp,f)
      # print(model_names[int(cip.split('\\')[5])])
      # print("Folder:",cip.split('\\')[4])
      # print("Folder Inside:",cip.split('\\')[5])
      f = open("car_modelss.txt", "a")
      model_names[1369][0][0].encode('utf-8') #here at this I get the specific array which I tried to convert hardcoded.
      f.write((str(model_names[int(cip.split('\\')[5])-1])))
      f.write("Folder: %d\r\n" % (int(cip.split('\\')[4])))
      f.write("Folder Inside: %d\r\n" % (int(cip.split('\\')[5])))
      count = count + 1
      print(count)

f.close()

请帮忙


Tags: 文件pathintxtmodelnamesoscount