如何让脚本遍历txt文件的每一行并执行一个函数?

时间:2017-06-22 12:54:57

标签: python python-3.x pandas

我正在尝试编写一个script来审核txt-file中的每个股票代码,并通过googlefinance来收集所有历史数据并将其输出到CSV-file {1}}。我知道这不应该那么困难,但我似乎无法弄清楚我在这段代码中做错了什么。它适用于第一批股票,然后崩溃。我假设它与\n有关,但我尝试了splitlines()函数和其他一些东西。

我的代码:

import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
from matplotlib.finance import candlestick_ohlc
import matplotlib.dates as mdates
import pandas as pd
import pandas_datareader.data as web
from googlefinance import getQuotes
import json
from datetime import datetime
from forex_python.converter import CurrencyRates
from yahoo_finance import Share

sp500 = open('sp500tickers.txt').read().splitlines()

for x in sp500:

    share = Share(sp500[x])
    print (share.get_price())

    start = dt.datetime(2001,1,1)
    end = dt.datetime(2017,1,1)

    df = web.DataReader(share, 'google', start, end)
    df.to_csv(''+x+'.csv', parse_dates=True, index_col=0)

文字文件

MMM
ABT
ABBV
ACN
ATVI
AYI
ADBE
AAP
AES
AET
AMG
AFL
A
APD
AKAM
ALK
ALB
ALXN
ALLE
AGN
ADS
LNT
ALL
GOOGL
GOOG
MO
AMZN
AEE
AAL
...

已编辑的代码:

with open('p1.txt', 'r') as f:
    for line in f:
        line = line.rstrip()

        share = Share(line)
        style.use('ggplot')
        print (share.get_price())

        start = dt.datetime(2001,1,1)
        end = dt.datetime(2017,1,1)

        df = web.DataReader(share, 'google', start, end)
        df.to_csv(''+line+'.csv', parse_dates=True, index_col=0)

2 个答案:

答案 0 :(得分:2)

在你for-loop中,x不是迭代器。 x是值本身,因此更改此行:

share = Share(sp500[x])

share = Share(x)

要阅读文件,您可以一次阅读每一行,而不是一次阅读所有内容,如下所示:

with open(file, 'r') as f:
    for line in f:
        line = line.rstrip()
        #your code

答案 1 :(得分:0)

1)你在这里做什么:

sp500 = open('sp500tickers.txt').read().splitlines()

正在将整个文件读入内存,这对于大文件来说是个坏主意 - 请改为:

with open('sp500tickers.txt') as sp500:

2)在这一行:

share = Share(sp500[i])

i来自哪里?

相关问题