将最后13行CSV复制到新的CSV

2024-03-29 07:01:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码将13家公司的最新股票信息附加到csv文件(更新的报价)。引号将添加到文件的底部。我只想复制最新的报价(13行)到一个新的csv文件,这将只为每个股票的最新报价一次。这将允许我只导入到excel文件的最新报价。有什么想法吗?你知道吗

import urllib.request
from bs4 import BeautifulSoup
import csv
from datetime import datetime
from urllib.request import Request, urlopen
from twilio.rest import Client
import os
import random

# list yahoo finance urls for desired stocks
yahoo_urls = ['https://finance.yahoo.com/quote/%5EDJI?p=^DJI', 'https://finance.yahoo.com/quote/%5ESPX?p=^SPX', 'https://finance.yahoo.com/quote/AAPL?p=AAPL', 'https://finance.yahoo.com/quote/KO/', 'https://finance.yahoo.com/quote/SBUX?p=SBUX', 'https://finance.yahoo.com/quote/DIS?p=DIS', 'https://finance.yahoo.com/quote/BRK-B?p=BRK-B', 'https://finance.yahoo.com/quote/NKE?p=NKE']

# loop for each stock with user-agent
for url in yahoo_urls:
    full_access = Request(url, headers={'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.20 (KHTML, like Gecko) Chrome/11.0.672.2 Safari/534.20'})

    # query the website and return the html to the variable ‘page’
    page = urllib.request.urlopen(full_access)

    # parse the html using beautiful soup and store in variable `soup`
    soup = BeautifulSoup(page, "html.parser")

    # Take out the <div> of name and get its value
    name_box = soup.find("h1", "D(ib) Fz(18px)")
    name = name_box.text.strip()
    print(name)

    #get the index price
    price_box = soup.find("span", "Trsdu(0.3s) Fw(b) Fz(36px) Mb(-4px) D(ib)")
    price = price_box.text.strip()
    print(price)

    # #Day Change
    try:
        change_box = soup.find("span", class_="Trsdu(0.3s) Fw(500) Pstart(10px) Fz(24px) C($dataGreen)")
        change = change_box.text.strip()
        print(change)
    except AttributeError:
        change_box = soup.find("span", class_="Trsdu(0.3s) Fw(500) Pstart(10px) Fz(24px) C($dataRed)")
        change = change_box.text.strip()
        print(change)

    # # open a csv file with append, so old data will not be erased
    with open("updated_quotes", "a") as csv_file:
     writer = csv.writer(csv_file)
     writer.writerow([name, price, change, datetime.now()])

Tags: 文件csvthenamehttpsimportcombox
2条回答

一个讨厌的解决办法就是只使用tail -n 13 updated_quotes.csv > last_quotes.csv。您可以通过subprocess.check_output("tail -n 13 updated_quotes.csv > last_quotes.csv", shell=True)调用它

您可以在csv读取器对象上使用deque尾部配方。你知道吗

我知道你有头球,留着吧。然后使用此处复制的tail recipe

def tail(filename, n=10):
    'Return the last n lines of a file'
    return deque(open(filename), n)

在interator上调用deque(如文件或csv读取器或任何其他形式的Python迭代器)等同于在同一对象上调用list(iterator)。但是使用deque可以限制大小并在该迭代器上创建Unix的tail实用程序的等价物。你知道吗

下面是一个使用大范围对象并仅保留最后5个对象的示例:

>>> from collections import deque
>>> deque(range(100000),5)
deque([99995, 99996, 99997, 99998, 99999], maxlen=5)

相关问题 更多 >