Path: csiph.com!usenet.pasdenom.info!gegeweb.org!newsfeed.kamp.net!newsfeed.kamp.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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '16,': 0.03; 'raises': 0.07; 'subject:Error': 0.07; 'subject:file': 0.07; '"w")': 0.09; 'subject:python': 0.11; 'csv': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'is",': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'return,': 0.16; 'subject:CSV': 0.16; 'subject:writing': 0.16; 'true:': 0.16; 'mon,': 0.16; 'wrote:': 0.17; 'feb': 0.19; 'trying': 0.21; 'import': 0.21; '"",': 0.22; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '(most': 0.27; 'logging': 0.27; '???': 0.27; "d'aprano": 0.29; 'steven': 0.29; 'error': 0.30; 'file': 0.32; 'received:84': 0.32; 'could': 0.32; 'print': 0.32; '11,': 0.33; 'like:': 0.33; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'pm,': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'data.': 0.36; 'anything': 0.36; 'possible': 0.37; 'does': 0.37; 'two': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'some': 0.38; 'nothing': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'end': 0.40; 'skip:w 30': 0.61; 'reached': 0.65; 'header:Reply-To:1': 0.68; 'programme': 0.69; 'reply-to:no real name:2**0': 0.72; '11:': 0.84; '12:': 0.84; '2013': 0.84; 'reply-to:addr:python.org': 0.84; 'theirs': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=HO4d4PRv c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=Tv4_tr9OjFAA:10 a=VyFPsJH9IiwA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=ZL2lUrT8GQwA:10 a=kZ7UWmmPAAAA:8 a=HGMg5HJ2J7CrQYpsQPMA:9 a=wPNLvfGTeEIA:10 a=pyH5b1fOeEsA:10 a=_1HZC5jcIYi8VP4x:21 a=QRm8LPZlrop7Qo1d:21 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Mon, 11 Feb 2013 13:02:35 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130107 Thunderbird/17.0.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Error in reading and writing CSV format file in python References: <5118e383$0$29985$c3e8da3$5496439d@news.astraweb.com> In-Reply-To: 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: 88 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360587743 news.xs4all.nl 6958 [2001:888:2000:d::a6]:55415 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38675 On 2013-02-11 12:44, inshu chauhan wrote: > > On Mon, Feb 11, 2013 at 1:26 PM, Steven D'Aprano > > wrote: > > inshu chauhan wrote: > > > In the programme below I am trying to read two csv format files and > > process them and write a new file with some of theirs data. > > > > import csv > > f1_reader = csv.reader(open(r"Z:\Weka > > work\Feature_Vectors_Fullset_00.arff")) > > f2_reader = csv.reader(open(r"Z:\Weka > > work\Feature_Vectors_Fullset_00_noxy+class.arff")) > > nf = open(r"Z:\Weka work\classified_image00_withoutxy.arff", "w") > > > > while True: > > l1 = f1_reader.next() > > while len(l1) != 12: > > l1 = f1_reader.next() > > l2 = f2_reader.next() > > while len(l2) != 11: > > l2 = f2_reader.next() > > > > ix = l1[0].strip() > > iy = l1[1].strip() > > classification = l2[8].strip() > > > > print >> nf, ix, iy, classification > > > > nf.close() > > > > This programme is giving me this error now : > > > > Traceback (most recent call last): > > File "Z:\Weka work\final_image_classificationwithoutxy.py", > line 16, in > > > > l2 = f2_reader.next() > > StopIteration > > > > > > what could be a possible reason to StopIteration ??? > > next() raises StopIteration when there is nothing else to return. > > > py> it = iter([1, 2, 3]) > py> it.next() > 1 > py> it.next() > 2 > py> it.next() > 3 > py> it.next() > Traceback (most recent call last): > File "", line 1, in > StopIteration > > > You have reached the end of the file and there is nothing else for > the CSV > reader to return, so it raises StopIteration. > > > > But why does it has nothing to return so early before traversing the > whole file ? Is there any way it can be corrected ? And also the > programme isn't writing anything to the file ? > Try adding some logging so that you can see what it's doing. A simple way would be something like: log_file = open(r"Z:\Weka work\log.txt", "w") ... l1 = f1_reader.next() print >> log_file, "Read from f1:", l1 print >> log_file, "Length is", len(l1) while len(l1) != 12: l1 = f1_reader.next() print >> log_file, "Read from f1:", l1 print >> log_file, "Length is", len(l1) and so on.