python如何读取和修改CSV

时间:2016-11-19 20:12:06

标签: python pandas

**iShares Russell 3000 ETF                                          
Inception Date  May 22, 2000                                        
Fund Holdings as of 31-oct-16                                       
Total Net Assets    $ 6,308,266,677                                     
Shares Outstanding  48,550,000                                      
Stock   -                                       
Bond    -                                       
Cash    -                                       
Other   -**                                     
                                           
Ticker  Name    Asset Class Weight (%)  Price   Shares  Market Value    Notional Value  Sector  SEDOL   ISIN    Exchange
AAPL    APPLE INC   Equity  2.8074  113.54  1,521,794   $ 172,784,491   172,784,490.76  Information Technology  2046251 US0378331005    NASDAQ
MSFT    MICROSOFT CORP  Equity  2.0474  59.92   2,103,008   $ 126,012,239   126,012,239.36  Information Technology  2588173 US5949181045    NASDAQ
XOM EXXON MOBIL CORP    Equity  1.5675  83.32   1,157,835   $ 96,470,812    96,470,812.20   Energy  2326618 US30231G1022    New York Stock Exchange Inc.
JNJ JOHNSON & JOHNSON   Equity  1.4378  115.99  762,927 $ 88,491,903    88,491,902.73   Health Care 2475833 US4781601046    New York Stock Exchange Inc.

我正在尝试删除基本信息并只读取下面的结构化数据,Pandas读取csv会抛出错误。

任何帮助都会非常有用。

1 个答案:

答案 0 :(得分:0)

我假设您要丢弃非csv标头,并且之后文件是制表符分隔的(它不能是空格,或者由于数据中缺少引号和空格而不是可解析的csv文件)

在这种情况下,您可以迭代直到找到标题,然后在文件上创建一个csv reader,如下所示:

import csv

with open("input.csv") as f:
    while(True):
        l=next(f)  # read input file lines
        if not l or l.startswith("Ticker"):  # title found (or end of file, safety)
            break

        cr = csv.reader(f,delimiter="\t")
        for row in cr:
            print(row)   # get valid rows