首页 > 解决方案 > UnicodeDecodeError:“charmap”编解码器无法解码位置 7458 中的字节 0x83:字符映射到

问题描述

我正在尝试使用 CSV 模块打开文件,但收到此错误

return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x83 in position 7458: character maps to

我检查了文件,文件编码是 UTF-8... 下面是我的代码。错误在第 63 行

import csv
import xml.etree.ElementTree as ET
import xml.dom.minidom as PT
import traceback

#Global variables
#Variable to hold file name
FILE_NAME = "CustomLabels.labels"
#Variable to hold delimiter
DELIMETE = ','
#Variable to hold root category in xml hierarchy
CUSTOM_LABELS = "CustomLabels"
#Variable to hold sub element in xml
LABELS = "labels"
#Variable to hold argument for CustomLabels category
XMLNS = 'xmlns'
#Variable to hold value for argument for CustomLabels category
URL = "http://soap.sforce.com/2006/04/metadata"
#variable to save file
SAVE_PATH = ""
#variable to hold file to read name
FILE_TO_READ = "CustomLabels.csv"


#Function to open the file with ugly XML
def openFile():
    print('D:M|***| openFile')
    try:
        customLabelsFile = open(FILE_NAME, 'r+',encoding="utf-8")
    except Exception:
        traceback.print_exc()
    return customLabelsFile

#Function to make pretty XML on output
def prettyXMLfile():
    print('D:M|***| prettyXMLfile')
    try:
        dom = PT.parse(FILE_NAME)
        pretty_xml_as_string = dom.toprettyxml()
    except Exception:
        traceback.print_exc()
    return pretty_xml_as_string


#Function to save preetyXML
#para
#xml_file - it is a file from openFile Function
#context - it is a formatted xml
def saveAsPrertyXML(xml_file,context):
    try:
        n = xml_file.write(context)
        xml_file.close()
    except Exception:
        traceback.print_exc()

with open(FILE_TO_READ,encoding="utf-8",errors='ignore',"rb") as csv_file:

    csv_reader = csv.reader(csv_file, encoding='utf-8',delimiter=DELIMETE)
    line_count = 0
    listOfColumnNames = list()
    customLabels = ET.Element(CUSTOM_LABELS)
    customLabels.set(XMLNS,URL)
    try:
        for row in csv_reader:
            if line_count == 0:
                listOfColumnNames.append(row)
                finalListOfColumns = listOfColumnNames[line_count]
                line_count += 1
            else:
                index = 0
                while index < len(finalListOfColumns):
                    if index == 0:
                        labels = ET.SubElement(customLabels, LABELS)
                    ET.SubElement(labels, finalListOfColumns[index]).text = row[index]
                    index += 1
                line_count += 1
    except Exception:
        print(f'The line with error is {line_count}')
        traceback.print_exc()

    tree = ET.ElementTree(customLabels)
    tree.write(FILE_NAME, xml_declaration=True,encoding='utf-8',method="xml")

    uglyXML = openFile()
    prettyXMLasString = prettyXMLfile()
    saveAsPrertyXML(uglyXML,prettyXMLasString)
    print(f'Generator pars {line_count} lines')
    print('XML file saved succesfull')

标签: pythonxmlcsv

解决方案


好的,我知道出了什么问题

它应该是:

with open(FILE_TO_READ,"rt",encoding="utf-8") as csv_file:

代替

with open(FILE_TO_READ,"rb+",encoding="utf-8") as csv_file:

推荐阅读