【问题标题】:Read in CSV trying to skip rows but having problems deleting first 6 rows读取 CSV 试图跳过行但在删除前 6 行时遇到问题
【发布时间】:2017-07-30 20:24:53
【问题描述】:

我正在尝试读取以下文件,但在读取 csv 时遇到问题。 CSV 文件在数据标题之前的文件顶部包含大量信息。我已经尝试过skirows,并且内容可以跳过文件顶部的内容,但它不起作用。

有人可以提供有关如何读取此文件的建议吗?

当前节目

import urllib
import pandas as pd
import StringIO
import datetime
import sys
if sys.version_info[0] < 3: 
    from StringIO import StringIO as stio
else:
    from io import StringIO as stio
myfile=[]
dls "http://www.spdrgoldshares.com/assets/dynamic/GLD/GLD_US_archive_EN.csv"
f = urllib.urlopen(dls)
myfile += f.readline()
TESTDATA=stio(myfile)
daily_prices = pd.read_csv(TESTDATA, sep=",", header=None,  skiprows=13, 
names=["Date", "GLD Close", "LBMA Gold Price", "NAV per GLD in Gold", 
"NAV/share at 10.30 a.m. NYT", "Indicative Price of GLD at 4.15 p.m. NYT",\
"Mid point of bid/ask spread at 4.15 p.m. NYT","Premium/Discount of GLD mid 
point v Indicative Value of GLD at 4.15 p.m. NYT",\
"Daily Share Volume","Total Net Asset Value Ounces in the Trust as at 4.15 
p.m. NYT", "Total Net Asset Value Tonnes in the Trust as at 4.15 p.m. NYT", 
"Total Net Asset Value in the Trust"])

在 csv 上的表头之前,文件中包含以下信息。我尝试使用跳过行和内容,但两者都不起作用。

SPDR Gold Shares(纽约证券交易所 Arca),

““SPDR”商标经 The McGraw-Hill Companies, Inc.(“McGraw-Hill”)许可使用。SPDR“Gold Trust 或其附属公司不提供任何金融产品的赞助、背书、销售或由 McGraw-Hill 推广。”

"注意:本文档仅供参考,如有更改,恕不另行通知。未经 SPDR Gold Shares spdrgoldshares@ssga.com 的书面许可,不得以任何方式复制本文档的任何部分。在任何情况下都不得它被用作或被视为出售要约或招揽购买其中提到的证券或其他工具的要约”

“注意:SPDR Gold Shares 并不表示此信息是准确或完整的,因此不应依赖此信息。SPDR Gold Shares 不对任何损失、损害、费用或索赔负责,无论以何种方式产生,因依赖此文件中包含的数据的结果。”

“注意:在未公布 LBMA 黄金价格的日期,使用最新的 LBMA 黄金价格。”

"*注:自 2015 年 3 月 20 日起,本信托一直使用 LBMA 下午黄金价格作为确定本信托黄金价值的黄金价格。在此之前,本信托使用伦敦 PM 定盘价,即已于 2015 年 3 月 19 日停产。所有对 LBMA 黄金价格的参考仅供参考。ICE benchmark Administration Limited 对价格或价格可能参考的基础产品的准确性不承担任何责任或义务。

【问题讨论】:

    标签: python pandas csv urllib stringio


    【解决方案1】:

    你可以使用:

    import requests
    from pandas.compat import StringIO
    dls = "http://www.spdrgoldshares.com/assets/dynamic/GLD/GLD_US_archive_EN.csv"
    
    r = requests.get(dls)
    daily_prices = pd.read_csv(StringIO(r.text), skiprows=6)
    

    print (daily_prices.head())
    
              Date  GLD Close  LBMA Gold Price  NAV per GLD in Gold  \
    0  18-Nov-2004      44.38          $442.00           100.000000   
    1  19-Nov-2004      44.78          $445.60            99.998900   
    2  22-Nov-2004      44.75          $447.80            99.995600   
    3  23-Nov-2004      45.05          $448.15            99.994500   
    4  24-Nov-2004      45.05          $448.60            99.993400   
    
       NAV/share at 10.30 a.m. NYT  Indicative Price of GLD at 4.15 p.m. NYT  \
    0                         44.2                                    44.305   
    1                  44.55951167                                    44.694   
    2                  44.77803823                                    44.903   
    3                  44.81255136                                    44.812   
    4                  44.85705902                                    44.952   
    
       Mid point of bid/ask spread at 4.15 p.m. NYT#  \
    0                                         $44.37   
    1                                         $44.78   
    2                                         $44.95   
    3                                         $44.74   
    4                                         $45.00   
    
       Premium/Discount of GLD mid point v Indicative Value of GLD at 4.15 p.m. NYT  \
    0                                             0.146%                              
    1                                             0.192%                              
    2                                             0.105%                              
    3                                            -0.160%                              
    4                                             0.095%                              
    
       Daily Share Volume  \
    0             5992000   
    1            11655000   
    2            11976800   
    3             3139000   
    4             6052700   
    
       Total Net Asset Value Ounces in the Trust as at 4.15 p.m. NYT  \
    0                                          260000.00               
    1                                         1859994.06               
    2                                         2799952.98               
    3                                         2799952.98               
    4                                         3099933.30               
    
       Total Net Asset Value Tonnes in the Trust as at 4.15 p.m. NYT  \
    0                                               8.09               
    1                                              57.85               
    2                                              87.09               
    3                                              87.09               
    4                                              96.42               
    
       Total Net Asset Value in the Trust  
    0                        114920000.00  
    1                        828806907.20  
    2                       1253785205.50  
    3                       1254751438.19  
    4                       1390568824.08  
    

    因为:

    daily_prices = pd.read_csv(dls, skiprows=6)
    print (daily_prices.head())
    

    HTTPError: 禁止

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多