Kael Tee Kael Tee - 1 year ago 73
Python Question

Fetching data till the end instead of using multiple next(v)[1]

Hi i have a python code that grab data from sample_data.csv before parsing them into out.csv.

Do see the image for better visualisation of sample_data.csv

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

datetime = next(v)[1],next(v)[1],next(v)[1],next(v)[1],next(v)[1]

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

transaction= next(v)[1], next(v)[1],

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)
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])

Answer Source

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

transaction = [ x[1] for x in v ]

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

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

 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:] ]