Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #38675

Re: Error in reading and writing CSV format file in python

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 <python@mrabarnett.plus.com>
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 <python@mrabarnett.plus.com>
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 <mailman.1634.1360580421.2939.python-list@python.org> <5118e383$0$29985$c3e8da3$5496439d@news.astraweb.com> <CAFqGZRHPtHRR8Sa5EgzMBxL12PX_S5ko4N0St1r6S5LQb2V78g@mail.gmail.com>
In-Reply-To <CAFqGZRHPtHRR8Sa5EgzMBxL12PX_S5ko4N0St1r6S5LQb2V78g@mail.gmail.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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1644.1360587743.2939.python-list@python.org> (permalink)
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

Show key headers only | View raw


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:
>
>     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
>      > <module>
>      >     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 "<stdin>", line 1, in <module>
>     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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Error in reading and writing CSV format file in python inshu chauhan <insideshoes@gmail.com> - 2013-02-11 12:00 +0100
  Re: Error in reading and writing CSV format file in python Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-11 23:26 +1100
    Re: Error in reading and writing CSV format file in python inshu chauhan <insideshoes@gmail.com> - 2013-02-11 13:44 +0100
    Re: Error in reading and writing CSV format file in python MRAB <python@mrabarnett.plus.com> - 2013-02-11 13:02 +0000
    Re: Error in reading and writing CSV format file in python inshu chauhan <insideshoes@gmail.com> - 2013-02-11 14:09 +0100

csiph-web