Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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; 'mrab': 0.05; 'raises': 0.07; 'subject:Error': 0.07; 'subject:file': 0.07; '"w")': 0.09; 'subject:python': 0.11; '>>': 0.16; 'csv': 0.16; 'is",': 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; 'to:name:python-list@python.org': 0.20; 'trying': 0.21; 'import': 0.21; '"",': 0.22; 'header:In-Reply- To:1': 0.25; '(most': 0.27; 'logging': 0.27; '???': 0.27; 'message-id:@mail.gmail.com': 0.27; "d'aprano": 0.29; 'steven': 0.29; 'url:mailman': 0.29; 'skip:& 10': 0.29; 'worked': 0.30; 'error': 0.30; 'url:python': 0.32; 'file': 0.32; 'could': 0.32; 'print': 0.32; 'url:listinfo': 0.32; '11,': 0.33; 'like:': 0.33; 'traceback': 0.33; 'to:addr:python-list': 0.33; 'skip:& 20': 0.33; 'received:google.com': 0.34; 'thanks': 0.34; 'pm,': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'url:org': 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; 'end': 0.40; 'url:mail': 0.40; '!!!': 0.62; 'reached': 0.65; 'programme': 0.69; '11:': 0.84; '12:': 0.84; '2013': 0.84; '2:02': 0.84; 'theirs': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=/TyG2sUTiRmWl2gUNu5V9AcipSE6Tw3S9dO4cyP0ANo=; b=WPPAb0W0F1cZuK5f6anGTo1tgqAco61DgIF1FrBwvZrFapzujxvEcOf3w2UeKJMnBl jAVd0++nIcMdhm2nls+bdkbIITD3encMpMHGNCTUny94EIg5Bk7f+24DslvpCR2Wbs5E lmBNRrl2ndSlZbtiCZUlZMdSKdLrRGMV7fQYvZvwVv54IMQm3rET5wWWBiFsIFvKSR+c jwclkmKBxokiD2VmQ0iyxO5ZG6Ku40Mbj742IhMfhvinDY1YpwxND2LMkoEo/XRpdkEh sPdqqILgMPYrhCytYFVXaWP2UJ2g1EeLhpNlan9s5q2V4kyTtxXF6TPKcsyYHesTuiui rPtw== MIME-Version: 1.0 X-Received: by 10.43.17.199 with SMTP id qd7mr17073800icb.52.1360588183109; Mon, 11 Feb 2013 05:09:43 -0800 (PST) In-Reply-To: <5118EBEB.4010302@mrabarnett.plus.com> References: <5118e383$0$29985$c3e8da3$5496439d@news.astraweb.com> <5118EBEB.4010302@mrabarnett.plus.com> Date: Mon, 11 Feb 2013 14:09:42 +0100 Subject: Re: Error in reading and writing CSV format file in python From: inshu chauhan To: "python-list@python.org" Content-Type: multipart/alternative; boundary=bcaec51968c95700c104d5729f71 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list 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: 227 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360588193 news.xs4all.nl 6967 [2001:888:2000:d::a6]:60241 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38676 --bcaec51968c95700c104d5729f71 Content-Type: text/plain; charset=ISO-8859-1 On Mon, Feb 11, 2013 at 2:02 PM, MRAB wrote: > 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. > -- > http://mail.python.org/**mailman/listinfo/python-list > Thanks :) This worked !!! --bcaec51968c95700c104d5729f71 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable


On Mon, Feb 11, 2013 at 2:02 PM, MRAB <python@mrabarnett.plus= .com> wrote:
On 2013-02-11 12:44, inshu= chauhan wrote:

On Mon, Feb 11, 2013 at 1:26 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info
<mailto:steve+comp.lang.python@pearwood.info>> wrote:<= br>
=A0 =A0 inshu chauhan wrote:

=A0 =A0 =A0> In the programme below I am trying to read two csv format f= iles and
=A0 =A0 =A0> process them and write a new file with some of theirs data.=
=A0 =A0 =A0>
=A0 =A0 =A0> import csv
=A0 =A0 =A0> f1_reader =3D csv.reader(open(r"Z:\Weka
=A0 =A0 =A0> work\Feature_Vectors_Fullset_00.arff"))
=A0 =A0 =A0> f2_reader =3D csv.reader(open(r"Z:\Weka
=A0 =A0 =A0> work\Feature_Vectors_Fullset_00_noxy+class.arff"= ;))
=A0 =A0 =A0> nf =3D open(r"Z:\Weka work\classified_image00_w= ithoutxy.arff", "w")
=A0 =A0 =A0>
=A0 =A0 =A0> while True:
=A0 =A0 =A0> =A0 =A0 l1 =3D f1_reader.next()
=A0 =A0 =A0> =A0 =A0 while len(l1) !=3D 12:
=A0 =A0 =A0> =A0 =A0 =A0 =A0 l1 =3D f1_reader.next()
=A0 =A0 =A0> =A0 =A0 l2 =3D f2_reader.next()
=A0 =A0 =A0> =A0 =A0 while len(l2) !=3D 11:
=A0 =A0 =A0> =A0 =A0 =A0 =A0 l2 =3D f2_reader.next()
=A0 =A0 =A0>
=A0 =A0 =A0> =A0 =A0 ix =3D l1[0].strip()
=A0 =A0 =A0> =A0 =A0 iy =3D l1[1].strip()
=A0 =A0 =A0> =A0 =A0 classification =3D l2[8].strip()
=A0 =A0 =A0>
=A0 =A0 =A0> =A0 =A0 print >> nf, ix, iy, classification
=A0 =A0 =A0>
=A0 =A0 =A0> nf.close()
=A0 =A0 =A0>
=A0 =A0 =A0> This programme is giving me this error now :
=A0 =A0 =A0>
=A0 =A0 =A0> Traceback (most recent call last):
=A0 =A0 =A0> =A0 File "Z:\Weka work\final_image_classificati= onwithoutxy.py",
=A0 =A0 line 16, in
=A0 =A0 =A0> <module>
=A0 =A0 =A0> =A0 =A0 l2 =3D f2_reader.next()
=A0 =A0 =A0> StopIteration
=A0 =A0 =A0>
=A0 =A0 =A0>
=A0 =A0 =A0> what could be a possible reason to StopIteration ???

=A0 =A0 next() raises StopIteration when there is nothing else to return.

=A0 =A0 py> it =3D iter([1, 2, 3])
=A0 =A0 py> it.next()
=A0 =A0 1
=A0 =A0 py> it.next()
=A0 =A0 2
=A0 =A0 py> it.next()
=A0 =A0 3
=A0 =A0 py> it.next()
=A0 =A0 Traceback (most recent call last):
=A0 =A0 =A0 =A0File "<stdin>", line 1, in <module> =A0 =A0 StopIteration


=A0 =A0 You have reached the end of the file and there is nothing else for<= br> =A0 =A0 the CSV
=A0 =A0 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 ? =A0And 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 w= ay
would be something like:

log_file =3D open(r"Z:\Weka work\log.txt", "w")

...

l1 =3D f1_reader.next()
print >> log_file, "Read from f1:", l1
print >> log_file, "Length is", len(l1)
while len(l1) !=3D 12:
=A0 =A0 l1 =3D f1_reader.next()
=A0 =A0 print >> log_file, "Read from f1:", l1
=A0 =A0 print >> log_file, "Length is", len(l1)

and so on.
--
http://mail.python.org/mailman/listinfo/python-list


Thanks :) This worked !!! =

--bcaec51968c95700c104d5729f71--