繁体   English   中英

如何从列表和csv文件创建[dict]

[英]How do I create [dict] from a list and csv file

我想创建一个词典,以便可以从列表(在这种情况下,从列表“ tweets”)中返回股票的值(在这种情况下,Rel Volume)。列表“ tweets”已从推特中删除,而Rel卷来自一个csv文件,其中的内容已从FinViz.com抓取,这是我的代码:

import csv
import urllib.request
from bs4 import BeautifulSoup

write_header = True

twiturl = "https://twitter.com/ACInvestorBlog"
twitpage = urllib.request.urlopen(twiturl)
soup = BeautifulSoup(twitpage,"html.parser")

print(soup.title.text)

tweets = [i.text for i in soup.select('a.twitter-cashtag.pretty-link.js-nav b')]
print(tweets)

url_base = "https://finviz.com/quote.ashx?t="
url_list = [url_base + tckr for tckr in tweets]

with open('_Stocks.csv', 'w', newline='') as file:
    writer = csv.writer(file)

    for url in url_list:
        try:
            fpage = urllib.request.urlopen(url)
            fsoup = BeautifulSoup(fpage, 'html.parser')

            # write header row (once)
            if write_header:
                writer.writerow(map(lambda e : e.text, fsoup.find_all('td', {'class':'snapshot-td2-cp'})))
                write_header = False

            # write body row
            writer.writerow(map(lambda e : e.text, fsoup.find_all('td', {'class':'snapshot-td2'})))
        except urllib.error.HTTPError:
            print("{} - not found".format(url))

with open('_Stocks.csv', 'r') as csv_file:
    csv_reader = csv.DictReader(csv_file)

    for line in csv_reader:
        print(line['Rel Volume'])

这是打印出来的内容:

Antonio Costa (@ACInvestorBlog) | Twitter
['AKS', 'X', 'TSLA', 'X', 'AKS', 'X', 'AKS', 'RNN', 'EKSO', 'LEDS', 'FORD', 'KDMN', 'CRMD', 'CRMD', 'CRMD', 'CRMD', 'CRMD', 'LEDS', 'SPX', 'SPX', 'SPY', 'VXX', 'UVXY', 'TVIX']
https://finviz.com/quote.ashx?t=SPX - not found
https://finviz.com/quote.ashx?t=SPX - not found
0.64
0.78
1.02
0.78
0.64
0.78
0.64
0.57
0.39
0.25
0.43
1.07
7.49
7.49
7.49
7.49
7.49
0.25
0.81
0.68
0.86
1.08

Process finished with exit code 0

因此,我想创建一个字典,其中“ AKS”等于值“ 0.64”

您所需要做的就是将名称列表映射为值! 因此zip可以轻松做到这一点。

# Rest of your code ..
rel_valume = []
for line in csv_reader:
    print(line['Rel Volume'])
    rel_valume.append(line['Rel Volume'])




my_dict = dict(zip(tweets, rel_valume))
Dict = {tweet:line['Rel Volume'] for (tweet, line) in zip(tweets, csv_reader)}

给出输出:

{'AKS': '0.78',
 'CRMD': '7.49',
 'EKSO': '0.57',
 'FORD': '0.25',
 'KDMN': '0.43',
 'LEDS': '7.49',
 'RNN': '0.64',
 'SPX': '0.81',
 'SPY': '0.68',
 'TSLA': '0.78',
 'UVXY': '1.08',
 'VXX': '0.86',
 'X': '0.64'}

除了创建字典外,您还可以将csv文件的第一列设置为tckr 然后,在写入文件的同时,将每个tckr写入第一列; 在阅读时,同时打印tckr和值。

另外,最好使用set而不是列表来保存tckrs,因为会有重复的结果。

为此,首先需要对代码进行一些更改。 无需事先创建网址列表,而是在循环内设置网址格式。 喜欢:

for tckr in tweets:
    URL = URL_BASE + tckr

这将有助于保存tckr值。

完整的代码:

write_header = True

twiturl = "https://twitter.com/ACInvestorBlog"
twitpage = urllib.request.urlopen(twiturl)
soup = BeautifulSoup(twitpage, "html.parser")

# use a set instead of a list to save the tckrs
tweets = {i.text for i in soup.select('a.twitter-cashtag.pretty-link.js-nav b')}

URL_BASE = "https://finviz.com/quote.ashx?t="

with open('_Stocks.csv', 'w', newline='') as file:
    writer = csv.writer(file)

    # note the change
    for tckr in tweets:
        URL = URL_BASE + tckr
        try:
            fpage = urllib.request.urlopen(URL)
            fsoup = BeautifulSoup(fpage, 'html.parser')

            if write_header:
                # note the change
                writer.writerow(['tckr'] + list(map(lambda e: e.text, fsoup.find_all('td', {'class': 'snapshot-td2-cp'}))))
                write_header = False

            # note the change
            writer.writerow([tckr] + list(map(lambda e: e.text, fsoup.find_all('td', {'class': 'snapshot-td2'}))))
        except urllib.request.HTTPError:
            print("{} - not found".format(URL))

with open('_Stocks.csv') as csv_file:
    csv_reader = csv.DictReader(csv_file)

    for line in csv_reader:
        print(line['tckr'], line['Rel Volume'])

输出:

https://finviz.com/quote.ashx?t=SPX - not found
TSLA 1.02
CRMD 7.49
EKSO 0.39
AKS 0.64
X 0.78
FORD 0.43
TVIX 1.08
SPY 0.81
VXX 0.68
RNN 0.57
LEDS 0.25
UVXY 0.86
KDMN 1.07

注意 writerow函数的参数更改。


如果仍然需要字典中的值,则可以使用以下命令:

with open('_Stocks.csv') as csv_file:
    csv_reader = csv.DictReader(csv_file)
    my_dict = {line['tckr']: line['Rel Volume'] for line in csv_reader}
    print(my_dict)

输出:

{'AKS': '0.64', 'X': '0.78', 'TSLA': '1.02', 'RNN': '0.57', 'EKSO': '0.39', 'LEDS': '0.25', 'FORD': '0.43', 'KDMN': '1.07', 'CRMD': '7.49', 'SPY': '0.81', 'VXX': '0.68', 'UVXY': '0.86', 'TVIX': '1.08'}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM