Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parsing csv using python
#1
I'm trying to parse a csv file using python. The csv file contains 2 columns with order_total and created_datetime.

[inline]24.99 2015-06-01 00:00:12
0 2015-06-01 00:03:15
164.45 2015-06-01 00:04:05
24.99 2015-06-01 00:08:01
0 2015-06-01 00:08:23
46.73 2015-06-01 00:08:51
0 2015-06-01 00:08:58
47.73 2015-06-02 00:00:25
101.74 2015-06-02 00:04:11
119.99 2015-06-02 00:04:35
38.59 2015-06-02 00:05:26
73.47 2015-06-02 00:06:50
34.24 2015-06-02 00:07:36
27.24 2015-06-03 00:01:40
82.2 2015-06-03 00:12:21
23.48 2015-06-03 00:12:35[/inline]

I need to print the total for each day. For instance the output should be -

2015-06-01 -> 261.16
2015-06-02 -> 415.75
2015-06-03 -> 132.92

Below is my code and its not working as expected -

def sum_orders_test(self,start_date,end_date):
        initial_date = datetime.date(int(start_date.split('-')[0]),int(start_date.split('-')[1]),int(start_date.split('-')[2]))
        final_date = datetime.date(int(end_date.split('-')[0]),int(end_date.split('-')[1]),int(end_date.split('-')[2]))
        day = datetime.timedelta(days=1)
        with open("file1.csv", 'r') as data_file:
            next(data_file)
            reader = csv.reader(data_file, delimiter=',')
            order_total=0
            if initial_date <= final_date:
                for row in reader:
                    if str(initial_date) in row[1]:
                        print 'initial_date : ' + str(initial_date)
                        print 'Date : ' + row[1]
                        order_total = order_total + row[0]
                    else:
                        print 'Else'
                        print 'Date ' + str(row[1]) + 'Total ' +str(order_total)
                        order_total=0
                        initial_date = initial_date + day 
Reply
#2
There's no comma in example
not working as expected  ... OK, so what's not working? ...
Reply
#3
In the given example there are -

1. 7 rows for 01-06-2015
2. 6 rows for 02-06-2015
3. 3 rows for 03-06-2015

When I run my code I get only result for -

1. 7 rows for 01-06-2015
2. 5 rows for 02-06-2015
3. 2 rows for 03-06-2015
Reply
#4
The following code will give you two lists within lists, one ascending on date, and one descending on date.
I formatted the CSV file as 3 columns, 1 for the float value, 1 for date, and 1 for time (attached my file)
If you only have two columns, will have to adjust.

code:
import operator
import csv


li = []
with open('sampleCSV.csv') as f:
    csvdata = csv.reader(f, delimiter=',')
    for row in csvdata:
        li.append(row)

# li sorted ascending on date (second column)
li_ascending = sorted(li, key=operator.itemgetter(1))
li_descending = sorted(li, key=operator.itemgetter(1), reverse=True)
print('Ascending: {}\nDescending: {}\n'.format(li_ascending, li_descending))
results:
Output:
Ascending: [['24.99', '2015-06-01', '00:00:12'], ['0', '2015-06-01', '00:03:15'], ['24.99', '2015-06-01', '00:08:01'], ['0', '2015-06-01', '00:08:23'], ['46.73', '2015-06-01', '00:08:51'], ['0', '2015-06-01', '00:08:58'], ['164.45', '2015-06-01 00:04:05'], ['47.73', '2015-06-02', '00:00:25'], ['101.74', '2015-06-02', '00:04:11'], ['119.99', '2015-06-02', '00:04:35'], ['38.59', '2015-06-02', '00:05:26'], ['73.47', '2015-06-02', '00:06:50'], ['34.24', '2015-06-02', '00:07:36'], ['27.24', '2015-06-03', '00:01:40'], ['82.2', '2015-06-03', '00:12:21'], ['23.48', '2015-06-03', '00:12:35']] Descending: [['27.24', '2015-06-03', '00:01:40'], ['82.2', '2015-06-03', '00:12:21'], ['23.48', '2015-06-03', '00:12:35'], ['47.73', '2015-06-02', '00:00:25'], ['101.74', '2015-06-02', '00:04:11'], ['119.99', '2015-06-02', '00:04:35'], ['38.59', '2015-06-02', '00:05:26'], ['73.47', '2015-06-02', '00:06:50'], ['34.24', '2015-06-02', '00:07:36'], ['164.45', '2015-06-01 00:04:05'], ['24.99', '2015-06-01', '00:00:12'], ['0', '2015-06-01', '00:03:15'], ['24.99', '2015-06-01', '00:08:01'], ['0', '2015-06-01', '00:08:23'], ['46.73', '2015-06-01', '00:08:51'], ['0', '2015-06-01', '00:08:58']]

Attached Files

.csv   sampleCSV.csv (Size: 422 bytes / Downloads: 216)
Reply
#5
I added some more code to show how to access the sorted list:

import operator
import csv


# li = [["user1", 100], ["user2", 234], ["user3", 131]]
li = []
with open('sampleCSV.csv') as f:
    csvdata = csv.reader(f, delimiter=',')
    for row in csvdata:
        li.append(row)
# li sorted ascending on date (second column)
li_ascending = sorted(li, key=operator.itemgetter(1))
li_descending = sorted(li, key=operator.itemgetter(1), reverse=True)
print('Ascending: {}\nDescending: {}\n'.format(li_ascending, li_descending))

# get all ascending grouped by date:
last_date = 0
for inner in li_ascending:
    if len(inner) != 3:
        break
    if last_date == 0:
        last_date == inner[1]

    if inner[1] != last_date:
        print('\n')
        last_date = inner[1]
    print('Value: {}, Date: {}, Time: {}'.format(inner[0], inner[1], inner[2]))
output:
Output:
Ascending: [['24.99', '2015-06-01', '00:00:12'], ['0', '2015-06-01', '00:03:15'], ['24.99', '2015-06-01', '00:08:01'], ['0', '2015-06-01', '00:08:23'], ['46.73', '2015-06-01', '00:08:51'], ['0', '2015-06-01', '00:08:58'], ['164.45', '2015-06-01 00:04:05'], ['47.73', '2015-06-02', '00:00:25'], ['101.74', '2015-06-02', '00:04:11'], ['119.99', '2015-06-02', '00:04:35'], ['38.59', '2015-06-02', '00:05:26'], ['73.47', '2015-06-02', '00:06:50'], ['34.24', '2015-06-02', '00:07:36'], ['27.24', '2015-06-03', '00:01:40'], ['82.2', '2015-06-03', '00:12:21'], ['23.48', '2015-06-03', '00:12:35']] Descending: [['27.24', '2015-06-03', '00:01:40'], ['82.2', '2015-06-03', '00:12:21'], ['23.48', '2015-06-03', '00:12:35'], ['47.73', '2015-06-02', '00:00:25'], ['101.74', '2015-06-02', '00:04:11'], ['119.99', '2015-06-02', '00:04:35'], ['38.59', '2015-06-02', '00:05:26'], ['73.47', '2015-06-02', '00:06:50'], ['34.24', '2015-06-02', '00:07:36'], ['164.45', '2015-06-01 00:04:05'], ['24.99', '2015-06-01', '00:00:12'], ['0', '2015-06-01', '00:03:15'], ['24.99', '2015-06-01', '00:08:01'], ['0', '2015-06-01', '00:08:23'], ['46.73', '2015-06-01', '00:08:51'], ['0', '2015-06-01', '00:08:58']] Value: 24.99, Date: 2015-06-01, Time: 00:00:12 Value: 0, Date: 2015-06-01, Time: 00:03:15 Value: 24.99, Date: 2015-06-01, Time: 00:08:01 Value: 0, Date: 2015-06-01, Time: 00:08:23 Value: 46.73, Date: 2015-06-01, Time: 00:08:51 Value: 0, Date: 2015-06-01, Time: 00:08:58
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Parsing link from html tags with Python Melcu54 0 1,622 Jun-14-2021, 09:25 AM
Last Post: Melcu54
  Is there a better way? Python parsing tlewick1 1 1,761 Oct-17-2020, 05:48 PM
Last Post: bowlofred
  XML Parsing in python aarushprathap 2 2,306 Jul-11-2020, 09:29 AM
Last Post: j.crater
  python realtime parsing logs anna 2 2,857 Jul-05-2020, 06:36 AM
Last Post: anna
  Parsing Text file having repeated value key pair using python manussnair 3 3,294 Aug-04-2018, 11:48 PM
Last Post: micseydel
  Python file parsing, can't catch strings in new line Philia 5 3,970 Apr-29-2018, 01:09 PM
Last Post: snippsat
  Text file parsing with python and with a list in grammar pitanga 2 3,237 Aug-31-2017, 02:21 PM
Last Post: pitanga

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020