Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.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文本文件-为dic中的单词指定数字_Python_Dictionary - Fatal编程技术网

Python文本文件-为dic中的单词指定数字

Python文本文件-为dic中的单词指定数字,python,dictionary,Python,Dictionary,我有一个txt文件,我正在从sec.gov下载,代码如下 import requests cik = requests.get('https://www.sec.gov/include/ticker.txt').text mylist = cik.split() 站点的输出如下所示: aapl 320193 msft 789019 amzn 1018724 goog 1652044 fb 1326801 tcehy 1293451 mylist = [

我有一个txt文件,我正在从sec.gov下载,代码如下

import requests

cik = requests.get('https://www.sec.gov/include/ticker.txt').text
mylist = cik.split()
站点的输出如下所示:

aapl    320193
msft    789019
amzn    1018724
goog    1652044
fb      1326801
tcehy   1293451
mylist = ["aapl    320193", "msft    789019", ...] # so on and so forth
mylist = [x.split() for x in mylist] # remove those excessive spaces
text = [x[0] for x in mylist] # grab the text
numbers = [x[1] for x in mylist] # grab the numbers
output_dict = dict(zip(text, numbers)) # magic!

>>> output_dict
{'aapl': '320193', 'msft': '789019', ...}
我将每个股票代码和编号拆分为如下字符串:
['aapl','320193','msft','789019','amzn','1018724','goog','1652044','fb','1326801']


如何将每个股票代码与以下数字组合?

我假设您的列表如下所示:

aapl    320193
msft    789019
amzn    1018724
goog    1652044
fb      1326801
tcehy   1293451
mylist = ["aapl    320193", "msft    789019", ...] # so on and so forth
mylist = [x.split() for x in mylist] # remove those excessive spaces
text = [x[0] for x in mylist] # grab the text
numbers = [x[1] for x in mylist] # grab the numbers
output_dict = dict(zip(text, numbers)) # magic!

>>> output_dict
{'aapl': '320193', 'msft': '789019', ...}

如果你有一个股票列表和另一个数字列表,你可以直接跳到
output\u dict
步骤。

我假设你问的是如何将两个列表映射到一个字典,以数字为键。这就是如何做到这一点:

word_lst = ["aapl", "msft", "amzn"]
num_list = [320193, 789019, 1018724] 
dic = {}

for i, item in enumerate(word_lst):
    dic[num_list[i]] = item


通过解析将提供的输出映射到字典的一种方法如下:

将文本输出转换为响应:

>>> import requests
>>> resp = requests.get("https://www.sec.gov/include/ticker.txt").text
现在,如果您检查第一行,您将看到它是
aap1\t320193\n
。这是有用的信息,有助于理解可以先按
\n
再按
\t
进行拆分,然后直接附加到字典中:

>>> combinations = resp.split("\n")
>>> dict_test = {}
>>> for element in combinations:
# Here, element.split("\t")[0] will be aap1 and element.split("\t")[1] will be 320193
    dict_test[element.split("\t")[0]] = element.split("\t")[1]
最后,由于字典本身太大,前5个输出如下所示:

>>> for idx, key in enumerate(dict_test.keys()):
    if idx == 5:
        break
    print("Key: {} and its Value: {}".format(key, dict_test[key]))
输出:

Key: aapl and its Value: 320193
Key: msft and its Value: 789019
Key: amzn and its Value: 1018724
Key: goog and its Value: 1652044
Key: fb and its Value: 1326801

StackOverflow不是免费的编码服务。你应该会的。请更新您的问题,以显示您已在某个应用程序中尝试过的内容。有关更多信息,请参阅并接受:)提示:
d[key]=value
将向字典添加条目
d
。在循环中读取每一行并将其拆分为键和值。嗨,Barmar,非常感谢你的建议。我是一名编码初学者,正在编写一些东西,遇到了一些我自己无法解决的问题,而谷歌也没有为我找到解决方案。@Likonima欢迎来到SO。你说你试过什么。我仍然看不到你的python代码,你在哪里?Hello@coldy。我没有提到要尝试什么。我提供的代码是requests.get(”,我在编辑中写道,我能够将所有这些内容分成一个列表。作为一个初学者,我现在的问题是,如果可以理解的话,我如何将像以前一样的每一秒都分配给该项。为什么不干脆
dict(mylist)
拆分后?将它们放入单独的列表中,然后使用
zip()将它们重新组合在一起
。它实际上不是一个真正的列表。相反,它是一个txt文件,我正从中提取。这就是我使用两个场景的原因。它实际上是一个我调用的网站,使用与我提供的格式相同的请求和.text方法。仅供参考:非常感谢您抽出时间。它确实回答了我的问题,可以理解。我还尝试按\n拆分,但不知道对于\t输出。如果您认为这已经回答了您的问题,您可以投票表决,谢谢