UnicodeEncodeError:'charmap'编解码器无法对位置9-10中的字符进行编码:字符映射到<undefined>

时间:2019-01-22 02:45:51

标签: 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()

请帮助

0 个答案:

没有答案