在python中获取错误

时间:2018-01-23 13:07:39

标签: python

在我的代码中出现以下错误。如何修复

    list_of_words = []
    pg_num = each_page.find('PageNumber').text
    zones = each_page.findall('Zone')
    for zone in zones:
        Zone_Number = zone.find('ZoneNumber').text
        lines = zone.findall('Line')
        for line in lines:
            LineNumber = line.find('LineNumber').text
            Line_Text = eol(line.find('OCRCharacters').text, zone_no=Zone_Number, line_number=LineNumber,
                            page_no=pg_num)
            list_of_words.append(Line_Text)
    join_all_vals = ''.join(list_of_words)
    replace_space = join_all_vals.replace(" ", "")
    get_each_alpha_count = Counter(replace_space)
    list_of_vals=[]
    print(type(get_each_alpha_count),get_each_alpha_count[0])
    for ke,va in get_each_alpha_count:
        new_format_val=ke + "|" + va
        list_of_vals.append(new_format_val)
    print(list_of_vals)
    exit()

Traceback (most recent call last):
  File "main.py", line 93, in <module>
    CT.pagenumber()
  File "C:\Users\karthik\uat\pyinstaller_testing_reference\three_pyinstaller_test\source\PageTraits\main.py", line 46, in pagenumber
    for ke,va in get_each_alpha_count:
ValueError: not enough values to unpack (expected 2, got 1)
Error in atexit._run_exitfuncs:
Traceback (most recent call last):

1 个答案:

答案 0 :(得分:2)

在迭代items()类似对象的Counter时使用dict

# ...
for ke, va in get_each_alpha_count.items():
    # ...

否则,您只是迭代键并且对两个变量的分配将失败,除非键本身是(长度为2)元组。您也可能想要考虑更好的字符串构建策略,因为您不能只连接intstr

new_format_val = "|".join(map(str, (ke, va)))
# OR
new_format_val = "{}|{}".format(ke, va)