试图创建索引,但变量无法达到定义的值[Python]

时间:2018-02-23 03:13:43

标签: python variables indexing

我正在尝试创建一个.txt文件的索引,其中包含所有大写的章节标题。我的尝试看起来像这样:

dictionary = {}
line_count = 0
for line in file:
    line_count += 1
    line = re.sub(r'[^a-zA-Z0-9 -]','',line)
    list = []
    if line.isupper():
       head = line
    else:
       list = line.split(' ')
       for i in list:
           if i not in stopwords:
               dictionary.setdefault(i, {}).setdefault(head, []).append(line_count)

然而,head变量找不到它的值,我试图将其分配给所有大写的行。我想要的输出是这样的:

>>dictionary['cat']
{'THE PARABLE': [3894, 3924, 3933, 3936, 3939], 'SNOW': [4501], 'THE CHASE': [6765, 6767, 6772, 6773, 6785, 6802, 6807, 6820, 6823, 6839]}

以下是数据的一部分:

THE GOLDEN BIRD

A certain king had a beautiful garden, and in the garden stood a tree
which bore golden apples. These apples were always counted, and about
the time when they began to grow ripe it was found that every night one
of them was gone. 

THE PARABLE

Influenced by those remarks, the bird next morning refused to bring in
the wood, telling the others that he had been their servant long enough,
and had been a fool into the bargain, and that it was now time to make a
change, and to try some other way of arranging the work. Beg and pray
as the mouse and the sausage might, it was of no use; the bird remained
master of the situation, and the venture had to be made. They therefore
drew lots, and it fell to the sausage to bring in the wood, to the mouse
to cook, and to the bird to fetch the water.

1 个答案:

答案 0 :(得分:0)

问题的核心是:

if line.isupper()

对于数字('0')或其他此类事情,此测试将失败。而是尝试:

if line.isupper() == line:

但一般来说,你的代码可能会使用像Pythonic这样的小小的爱:

import re
data = {}
head = ''
with open('file1', 'rU') as f:
    for line_num, line in enumerate(f):
        line = re.sub(r'[^a-zA-Z0-9 -]', '', line)
        if line.isupper() == line:
            head = line
        else:
            for word in (w for w in line.split(' ') if w not in stopwords):
                data.setdefault(word, {}).setdefault(head, []).append(line_num+1)