首页 > 解决方案 > UnicodeDecodeError:“utf-8”编解码器无法解码位置 0 的字节 0xc4:

问题描述

我是 python 新手。我创建了一个代码来计算 YOLO 注释中包含多少类,我遇到了这个错误。

在此处输入图像描述

我真的不熟悉这个编码错误。`_' 这个我找了很多,但是没有'0xc4'的解决方案。

首先,我添加我的整个代码。

mid_num = 4000
total_num = 13485     #총 이미지 개수
dir = '/media/aicar/Seagate Backup Plus Drive/최종_데이터셋_cone/labels/'         #어노테이션이 있는 위치
# dir = '/home/aicar/labelImg-master/data/'

cone = 0
stick = 0
crossline = 0 
stopline = 0
red_light = 0
yellow_light = 0 
green_light = 0
str_and_left = 0
left_turn = 0
drum = 0


for idx in range(1, mid_num+1):

    # filename = dir + str(idx) + '.txt' 
    filename = dir + 'corn_' + str(idx) + '.txt' 

    # f = open(filename, 'r')
    

    col_2=[]

    with open(filename) as f:
        for line in f:
            chars=[]
            line=line.split(' ')
            for char in line:
                if char not in ['',' ']:
                    chars.append(char)
            col_2.append(chars[0])

        f.close()    

    # print(col_2)


    
    if '0' in col_2:
        cone = cone +1

    if '1' in col_2:
        stick = stick +1

    if '2' in col_2:
        crossline = crossline +1

    if '3' in col_2:
        stopline = stopline +1

    if '4' in col_2:
        red_light = red_light +1
    
    if '5' in col_2:
        yellow_light = yellow_light +1

    if '6' in col_2:
        green_light = green_light +1

    if '7' in col_2:
        str_and_left = str_and_left +1

    if '8' in col_2:
        left_turn = left_turn +1

    if '9' in col_2:
        drum = drum +1

    

print('cone : ' + str(cone))
print('stick : ' + str(stick))
print('crossline : ' + str(crossline))
print('stopline : ' + str(red_light))
print('red_light : ' + str(red_light))
print('yellow_light : ' + str(yellow_light))
print('green_light : ' + str(green_light))
print('str_and_left : ' + str(str_and_left))
print('left_turn : ' + str(left_turn))
print('drum : ' + str(drum))

我拥有的锡总数为 13,485,显示在第 5 行的“total_num”中。

如果我在 13,485 个注释上运行此代码,则会出现 UnicodeDecodeError,如果我运行大约 1,000 个,它可以正常工作。

下图是代码开启只有1000个注解时的终端窗口。

在此处输入图像描述

我需要专家的帮助。谢谢。

标签: pythonutf-8python-unicode

解决方案


推荐阅读