Fetching data till the end instead of using multiple next(v)[1]
<p>Hi i have a python code that grab data from sample_data.csv before parsing them into out.csv.</p>

<p>Do see the image for better visualisation of sample_data.csv
<a href="http://i.imgur.com/wwi4RC7.jpg" rel="nofollow">http://i.imgur.com/wwi4RC7.jpg</a></p>

<p>My question is how do i start from the the last next(v)[1] in</p>

<pre><code>datetime = next(v)[1],next(v)[1],next(v)[1],next(v)[1],next(v)[1]
</code></pre>

<p>and begin all the way till the end of the line instead of being silly and using multiple next(v)[1]? This is an issue as different receipt has different amount of line thus i cant have a fix number of next(v)[1] for transaction</p>

<blockquote>
  <p>transaction= next(v)[1], next(v)[1],
  next(v)[1],next(v)[1],next(v)[1],next(v)[1],next(v)[1],next(v)[1]</p>
</blockquote>

<pre><code>import csv
    from itertools import groupby
    from operator import itemgetter
    import re

    with open("sample_data.csv", "rb") as f, open("out.csv", "wb") as out:
        reader = csv.reader(f)
        next(reader)
        writer = csv.writer(out)
        writer.writerow(["Receipt ID","Name","Address","Date","Time","Items","Amount","Cost","Total"])
        groups = groupby(csv.reader(f), key=itemgetter(0))
        for k, v in groups:
            id_, name = next(v)
            add_date_1, add_date_2 = next(v)[1], next(v)[1]
            combinedaddress = add_date_1+ " " +add_date_2
            datetime = next(v)[1],next(v)[1],next(v)[1],next(v)[1],next(v)[1]
            abcd = str(datetime)
            dateprinter = re.search('(\d\d/\d\d/\d\d\d\d)\s(\d\d:\d\d)', abcd).group(1)
            timeprinter = re.search('(\d\d/\d\d/\d\d\d\d)\s(\d\d:\d\d)', abcd).group(2)

            transaction= next(v)[1], next(v)[1], next(v)[1],next(v)[1],next(v)[1],next(v)[1],next(v)[1],next(v)[1]
            writer.writerow([id_, name, combinedaddress, dateprinter, timeprinter, transaction])
</code></pre>

<p>If I understand the question correctly, you could use a list comprehension to finish reading values in v , like that :</p>

<pre><code>transaction = [ x[1] for x in v ]
</code></pre>

<p>That code will be like grabing all remaining  <code>next(v)[1]</code> until the end of v.</p>

<p><strong>Side note</strong> : calling next(v) all the time is quite ugly and unpractical, you could instead start by converting <code>v</code> to a list and then use simple list slicing to get what you want :</p>

<pre><code> v = list(v)
 id_, name = v[0]
 add_date_1, add_date_2 = [x[1] for x in v[1:3]]
 ...
 transaction = [ x[1] for x in v[1234:] ]
</code></pre>

python csv