Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在迭代多个for循环时创建字典?_Python_Python 2.7_For Loop_Dictionary_Beautifulsoup - Fatal编程技术网

Python 在迭代多个for循环时创建字典?

Python 在迭代多个for循环时创建字典?,python,python-2.7,for-loop,dictionary,beautifulsoup,Python,Python 2.7,For Loop,Dictionary,Beautifulsoup,我把总统演讲的日期和每一次演讲的文件名都存储在一本字典里。speechs对象如下所示: [<a href="/president/obama/speeches/speech-4427">Acceptance Speech at the Democratic National Convention (August 28, 2008)</a>, <a href="/president/obama/speeches/speech-4424">Remarks on E

我把总统演讲的日期和每一次演讲的文件名都存储在一本字典里。
speechs
对象如下所示:

[<a href="/president/obama/speeches/speech-4427">Acceptance Speech at the Democratic National Convention (August 28, 2008)</a>,
<a href="/president/obama/speeches/speech-4424">Remarks on Election Night (November 4, 2008)</a>,...]
date_dict[date] = filename
date_dict['key'] = value
这是我的密码:

date_dict = {}
for speech in speeches:
    text = speech.get_text(strip=True)
    date = text[text.index("(") + 1:text.rindex(")")]
    end_link = [tag.get('href') for tag in speeches if tag.get('href') is not None]
    for x in end_link:
        splitlink = x.split("/")
        president = splitlink[2]
        speech_num = splitlink[4]
        filename = "{0}_{1}".format(president,speech_num)
        if 2 == 2:
            f = date_dict['{0} {1}'.format(filename,date)]
我得到了正确的日期输出(例如1999年8月15日<代码>文件名<代码>很好)。现在,我正试图将这两种方法结合起来,并得到以下错误:

date_dict['{0} {1}'.format(filename,date)]
KeyError: 'obama_speech-4427 August 28, 2008'

我真的不知道该从这里走到哪里。

您没有将该键的值设置为任何值,因此Python认为您正在尝试读取该键。日期字典是空的

您需要设置一个值,如下所示:

[<a href="/president/obama/speeches/speech-4427">Acceptance Speech at the Democratic National Convention (August 28, 2008)</a>,
<a href="/president/obama/speeches/speech-4424">Remarks on Election Night (November 4, 2008)</a>,...]
date_dict[date] = filename
date_dict['key'] = value
字典有键和值。要分配给字典,您可以执行以下操作:

[<a href="/president/obama/speeches/speech-4427">Acceptance Speech at the Democratic National Convention (August 28, 2008)</a>,
<a href="/president/obama/speeches/speech-4424">Remarks on Election Night (November 4, 2008)</a>,...]
date_dict[date] = filename
date_dict['key'] = value
连接部分没有问题<代码>'{0}{1}'。格式(文件名、日期)可以,不过您可能需要下划线而不是空格。如果这将在网站上发布,也可能是一个短跑

编辑

根据我们的讨论,我认为您需要这样做:

date_dict = {}
for speech in speeches:
    text = speech.get_text(strip=True)
    date = text[text.index("(") + 1:text.rindex(")")]
    end_link = [tag.get('href') for tag in speeches if tag.get('href') is not None]
    for x in end_link:
        splitlink = x.split("/")
        president = splitlink[2]
        speech_num = splitlink[4]
        filename = "{0}_{1}".format(president,speech_num)
        if 2 == 2:
            date_dict[filename] = date

# Prints name date for a given file(just an example)
print("File", filname, "recorded on", date_dict[filename]) 

我明白你的意思,但我还是得到了同样的错误,即使我将
date\u dict[…
分配给变量注意,在一般情况下,转换为字符串可能会出现错误,您可以使用
tuple
键避免这些错误。
date\u dict[filename,date]=end_link
避免了字符串格式化开销,同样合法,并将密钥的组件分开,这样您可以在迭代时单独使用每个组件,而不是试图解析字符串。如果需要将密钥用于输出,您可以在以后对其进行格式化。注意。谢谢。一旦获得日期,我将担心格式化问题或正确。我只需要能够通过
filename
访问语音并获取该语音的日期。太好了。感谢您的帮助。顺便说一句,我在前面的
for
循环中获得了
filename
以输出到词典,所以我要看看是否可以将
date
指定为该词典中的值谢谢你的帮助。我希望值是演讲的日期,键是文件名。我将通过将文件名作为连接键连接,将日期与其他一些数据进行匹配。你需要设置字典槽的值。字典的关联键与值相关。像邮箱号与邮箱内的内容相关。如果y如果要保存文件名,您希望密钥是什么?语音编号可能会起作用。文件名是由
为x在end_链接中生成的
文件名=“{0}{1}”。格式(主席,语音编号)
。例如
文件名
将是
obama_speech-4427
。然后,我想将其与2008年8月28日的
配对。你为什么想要一本词典?一份列表可能更好?我现在知道你的代码的功能,但我不确定它是否能满足你的要求。当你填写完词典后,你会用它做什么
filename
date
已保存(在字典或列表中-只要文件名与日期正确匹配,我就不在乎了(即,奥巴马演讲的
filename
不应该有肯尼迪演讲的日期)。我认为dict是最好的方法。