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

时间:2018-07-05 19:48:36

标签: python csv

我的代码将13家公司的最新股票信息附加到csv文件中(updated_quotes)。引号添加到文件的底部。我只想将最新报价(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()])

2 个答案:

答案 0 :(得分:1)

您可以在csv阅读器对象上使用interface Base<Child extends Base = { type: string, child?: Base }> { type: string; child?: Child; } interface Ext extends Base { type: 'test'; sth: string; } z({ type: 'a', child: { type: 'b', } }); // ok z({ type: 'a', child: { type: 'test', sth: 'val' } }); // not ok function z<B extends Base>(input: B) { } 尾部配方。

我有一个标题,请保留。然后使用此处复制的tail recipe

deque

在Interator上调用def tail(filename, n=10): 'Return the last n lines of a file' return deque(open(filename), n) (例如文件或csv阅读器或任何其他形式的Python迭代器)等效于在同一对象上调用deque。但是,使用list(iterator)可以限制大小,并在该迭代器上创建与Unix的deque等效的工具。

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

tail

答案 1 :(得分:-1)

一个讨厌解决方案是仅使用tail -n 13 updated_quotes.csv > last_quotes.csv。您可以通过subprocess.check_output("tail -n 13 updated_quotes.csv > last_quotes.csv", shell=True)

调用它