中止陷阱:在CSV3中下载数据时在Python3中为6

时间:2017-10-08 15:39:48

标签: python-3.x csv

我正在尝试下载Nifty 50公司的数据并将其作为CSV存储在我的硬盘上。我使用下面提到的代码。它成功下载了32家公司的数据,但之后它开始给出错误=“python3(820,0x70000950d000)malloc: *错误对象0x7f9eba7103b0:被释放的指针未被分配 * 在malloc_error_break中设置断点以进行调试 中止陷阱:6“。我在macbook上并使用终端。

import csv
import urllib.request as urllib
import datetime as dt
import os
import pandas as pd
import pandas_datareader.data as web
import nsepy as nse

def saveNiftySymbols():
    url = "https://www.nseindia.com/content/indices/ind_nifty50list.csv"
# pretend to be a chrome 47 browser on a windows 10 machine
    headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"}
    req = urllib.Request(url, headers = headers)
# open the url 
    x = urllib.urlopen(req)
    sourceCode = x.read().decode('utf-8') 

    cr = csv.DictReader(sourceCode.splitlines())
    l = [row['Symbol'] for row in cr]
    return l

def fetchDataFromNse(l):
    if not os.path.exists('stock_dfs'):
        os.makedirs('stock_dfs')

    start = dt.datetime(2000, 1, 1)
    end = dt.datetime.today()

    for symbol in l:
        if not os.path.exists('stock_dfs/{}.csv'.format(symbol)):
            df=nse.get_history(symbol,start, end)
            df.to_csv('stock_dfs/{}.csv'.format(symbol))
        else:
            print('Already have {}'.format(symbol))

fetchDataFromNse(saveNiftySymbols())

1 个答案:

答案 0 :(得分:1)

这似乎是一个与mac上python安装相关的bug,查看这篇文章,它有点旧但是问题是一样的 https://github.com/ludwigschwardt/python-gnureadline/issues/50