Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'encoding': 0.05; '"""': 0.07; 'encoded': 0.07; 'skip:\\ 20': 0.07; 'utf-8': 0.07; 'holdings': 0.09; 'indicates': 0.09; 'subject:skip:c 10': 0.09; 'try:': 0.09; 'subject:question': 0.10; 'python': 0.11; 'def': 0.12; 'windows': 0.15; '"\\r\\n"': 0.16; 'csv': 0.16; 'exit()': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'ioerror:': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'nameerror:': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'skip:\\ 30': 0.16; 'skip:d 150': 0.16; 'skip:g 190': 0.16; 'skip:{ 30': 0.16; '{0}': 0.16; 'wrote:': 0.18; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'finally,': 0.24; 'skip:{ 20': 0.24; 'script': 0.25; 'header:In-Reply-To:1': 0.27; 'skip:p 30': 0.29; 'characters': 0.30; 'lines': 0.31; "skip:' 10": 0.31; 'cap': 0.31; 'keys': 0.31; 'skip:s 70': 0.31; 'anyone': 0.31; 'file': 0.32; 'text': 0.33; 'open': 0.33; 'running': 0.33; 'reader': 0.33; 'created': 0.35; 'except': 0.35; 'skip:s 30': 0.35; 'received:84': 0.35; 'test': 0.35; 'there': 0.35; 'skip:" 50': 0.36; 'to:addr :python-list': 0.38; 'explain': 0.39; 'to:addr:python.org': 0.39; 'skip:a 30': 0.61; 'first': 0.61; 'field': 0.63; 'holding': 0.65; 'here': 0.66; 'header:Reply-To:1': 0.67; 'results': 0.69; 'reply- to:no real name:2**0': 0.71; 'present.': 0.74; 'presumably': 0.84; 'reply-to:addr:python.org': 0.84; 'skip:v 50': 0.84; 'start.': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=ZarDwLpA c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=QUiRZEtCktgA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=PH9ef1eSHucA:10 a=uNu8z-BqOnkmRDrm7c8A:9 a=wPNLvfGTeEIA:10 a=6h_qtuMUb10A:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Thu, 04 Apr 2013 03:46:18 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130328 Thunderbird/17.0.5 MIME-Version: 1.0 To: python-list@python.org Subject: Re: question about csv.DictReader References: <846339ea-366a-4bb2-b234-0e03bf87489e@googlegroups.com> In-Reply-To: <846339ea-366a-4bb2-b234-0e03bf87489e@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 72 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365043571 news.xs4all.nl 6931 [2001:888:2000:d::a6]:44733 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:42730 On 04/04/2013 02:26, Norman Clerman wrote: > Hello, > > I have the following python script (some of lines are wrapped): > > #! /usr/bin/env python > > import csv > > def dict_test_1(): > """ csv test program """ > > # Open the file Holdings_EXA.csv > HOLDING_FILE = 'Holdings_EXA.csv' > try: > csv_file = open(HOLDING_FILE, 'rt') > except IOError: > print('Problem opening {0}\nExiting').format(HOLDING_FILE) > exit() > > # create a dictionary reader > try: > csv_reader = csv.DictReader(csv_file) > except NameError: > print('Cannot find file {0} to create a dictionary reader \nExiting').format(HOLDING_FILE) > exit() > > # Print the keys in each row > i_row = 1 > for row in csv_reader: > print ('There are {0} keys in row {1}').format(len(row.keys()), i_row) > print ('The keys in row {0} are \n{1}').format(i_row, row.keys()) > i_row += 1 > dict_test_1() > > Here are the lines in file Holdings_EXA.csv: > Please note that the first field in the first row is "Holdings" > > "Holdings","Weighting","Type","Ticker","Style","First Bought","Shares Owned","Shares Change","Sector","Price","Day Change","Day high/low","Volume","52-Wk high/low","Country","3-Month Return","1-Year Return","3-Year Return","5-Year Return","Market Cap Mil","Currency","Morningstar Rating","YTD Return","P/E","Maturity Date","Coupon %","Yield to Maturity" > "Nestle SA","1.91","EQUITY","NESN","Large Core","1999-12-31","3732276","197810","Consumer Defensive","67.65","-","67.75-67.35","1211531","67.75-53.8","Switzerland","10.42","21.25","10.5","8.84","213475.59","CHF","2","12.92","21.69","-","-","-" > "HSBC Holdings PLC","1.75","EQUITY","HSBA","Large Value","1999-12-31","21120203","1711934","Financial Services","733.3","-1.4|-0","738.8-731","7839724","739.9-501.2","United Kingdom","14.51","37.17","3.88","2.77","132694.66","GBP","3","13.93","15.55","-","-","-" > "Novartis AG","1.33","EQUITY","NOVN","Large Core","2003-06-30","2669523","206851","Healthcare","65.95","0.5|0.01","66-65.4","1121549","66-48.29","Switzerland","15.1","36.5","6.16","8.53","158671.66","CHF","4","16.7","17.76","-","-","-" > "Roche Holding AG","1.31","EQUITY","ROG","Large Growth","2003-05-31","817830","59352","Healthcare","214.8","1.4|0.01","215.2-213.1","684173","220.4-148.4","Switzerland","17.45","37.95","7.78","4.09","34000","CHF","3","18.09","19.05","-","-","-" > > Finally, here are the results of running the script: > > > norm@lima:~/python/overlap$ python dict_test_1.py > There are 27 keys in row 1 > The keys in row 1 are > ['Style', 'Day Change', 'Coupon %', 'Yield to Maturity', 'P/E', 'Type', 'Weighting', 'Price', '3-Month Return', 'Volume', '\xef\xbb\xbf"Holdings"', 'Ticker', 'Shares Change', 'Shares Owned', 'YTD Return', '5-Year Return', 'Market Cap Mil', 'Country', '3-Year Return', 'Day high/low', 'Maturity Date', '1-Year Return', 'Sector', 'Morningstar Rating', 'Currency', '52-Wk high/low', 'First Bought'] > There are 27 keys in row 2 > The keys in row 2 are > ['Style', 'Day Change', 'Coupon %', 'Yield to Maturity', 'P/E', 'Type', 'Weighting', 'Price', '3-Month Return', 'Volume', '\xef\xbb\xbf"Holdings"', 'Ticker', 'Shares Change', 'Shares Owned', 'YTD Return', '5-Year Return', 'Market Cap Mil', 'Country', '3-Year Return', 'Day high/low', 'Maturity Date', '1-Year Return', 'Sector', 'Morningstar Rating', 'Currency', '52-Wk high/low', 'First Bought'] > There are 27 keys in row 3 > The keys in row 3 are > ['Style', 'Day Change', 'Coupon %', 'Yield to Maturity', 'P/E', 'Type', 'Weighting', 'Price', '3-Month Return', 'Volume', '\xef\xbb\xbf"Holdings"', 'Ticker', 'Shares Change', 'Shares Owned', 'YTD Return', '5-Year Return', 'Market Cap Mil', 'Country', '3-Year Return', 'Day high/low', 'Maturity Date', '1-Year Return', 'Sector', 'Morningstar Rating', 'Currency', '52-Wk high/low', 'First Bought'] > There are 27 keys in row 4 > The keys in row 4 are > ['Style', 'Day Change', 'Coupon %', 'Yield to Maturity', 'P/E', 'Type', 'Weighting', 'Price', '3-Month Return', 'Volume', '\xef\xbb\xbf"Holdings"', 'Ticker', 'Shares Change', 'Shares Owned', 'YTD Return', '5-Year Return', 'Market Cap Mil', 'Country', '3-Year Return', 'Day high/low', 'Maturity Date', '1-Year Return', 'Sector', 'Morningstar Rating', 'Currency', '52-Wk high/low', 'First Bought'] > norm@lima:~/python/overlap$ > > > Can anyone explain the presence of the characters "\xref\xbb\xbf" before the first field contents "Holdings" ? > Microsoft Windows indicates that a text file contains text encoded as UTF-8 by including a signature at its start. (Does the file also have "\r\n" line endings? Presumably it was created on a Windows system.) Try opening the file with the "utf-8-sig" encoding instead; this will drop the signature if present.