Python Forum
Getting duplicate column values but same row values from a csv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting duplicate column values but same row values from a csv
#1
I am SO STUCK and its driving me mad. I have a CSV like so:

Quote:date, order_id, sku, transaction_type, payment_type, payment_detail, amount, quantity
20 Nov 2019 026-8286010-8145911 P6-VZ65-2FQT Order Payment Amazon fees Commission £-5.82
20 Nov 2019 026-8286010-8145911 P6-VZ65-2FQT Order Payment Amazon fees FBA fulfilment fee per unit £-7.73
20 Nov 2019 026-8286010-8145911 P6-VZ65-2FQT Order Payment Product charges £40.42 1
20 Nov 2019 026-8286010-8145911 P6-VZ65-2FQT Order Payment Promo rebates £0.00 1
20 Nov 2019 026-8286010-8145911 P6-VZ65-2FQT Order Payment Other Product Tax £8.08
20 Nov 2019 026-8286010-8145911 P6-VZ65-2FQT Order Payment Amazon fees Commission £-5.82
20 Nov 2019 026-8286010-8145911 P6-VZ65-2FQT Order Payment Amazon fees FBA fulfilment fee per unit £-7.73
20 Nov 2019 026-8286010-8145911 P6-VZ65-2FQT Order Payment Product charges £40.42 1
20 Nov 2019 026-8286010-8145911 P6-VZ65-2FQT Order Payment Promo rebates £0.00 1
20 Nov 2019 026-8286010-8145911 P6-VZ65-2FQT Order Payment Other Product Tax £8.08
19 Nov 2019 206-3581742-6918727 N4-AMGB-L9HS Order Payment Amazon fees Commission £-8.58
19 Nov 2019 206-3581742-6918727 N4-AMGB-L9HS Order Payment Amazon fees FBA fulfilment fee per unit £-5.97
19 Nov 2019 206-3581742-6918727 N4-AMGB-L9HS Order Payment Amazon fees Shipping chargeback £-0.93
19 Nov 2019 206-3581742-6918727 N4-AMGB-L9HS Order Payment Product charges £59.58 1
19 Nov 2019 206-3581742-6918727 N4-AMGB-L9HS Order Payment Other Product Tax £11.92
19 Nov 2019 206-3581742-6918727 N4-AMGB-L9HS Order Payment Other Shipping tax £0.19
19 Nov 2019 206-3581742-6918727 N4-AMGB-L9HS Order Payment Other Shipping £0.93
1

import csv

order_ids = []
info = []

with open('report6.csv', mode='r', encoding='utf-8') as infile:
    product_charges = ""
    product_tax = ""
    reader = csv.reader(infile)
    data = [rows for rows in reader]
    skipped = data[4:]
    for i in skipped:
        order_id = i[1]
        if order_id in order_ids:
            sku = i[2]
            transaction_type = i[3]
            payment_type = i[4]
            if payment_type == "Product charges":
                product_charges = i[6]
                product_charges = product_charges.replace("£", "")
            payment_detail = i[5]
            if payment_detail == "Product Tax":
                product_tax = i[6]
                product_tax = product_tax.replace("£", "")

            format = [order_id, [product_charges, product_tax]]
            info.append(format)
            continue
        else:
            order_ids.append(order_id)
            sku = i[2]
            transaction_type = i[3]
            payment_type = i[4]
            if payment_type == "Product charges":
                product_charges = i[6]
                product_charges = product_charges.replace("£", "")
            payment_detail = i[5]
            if payment_detail == "Product Tax":
                product_tax = i[6]
                product_tax = product_tax.replace("£", "")

            format = [order_id, [product_charges, product_tax]]
            info.append(format)
            continue

for a in range(0, 1, len(order_ids)):
    for b in info:
        while order_ids[a] == b[0]:
            a +=1
And all the data per order_id is on a separate row, and I am trying to get it all on the same row - in particular, the product_charges and product_tax.
For some orders, there is duplicate amounts of product_charges and product_taxes - and I just dont know how to handle these...

I want my output to be like:
Output:
026-8286010-8145911, 80.82, 16.16 206-3581742-6918727, 59.58, 11.92
Any help would be immensely appreciated!
Reply
#2
This reads every row at once, which is OK if that's what you intended

also be aware that your skipped is skipping the entire the 5th row and beyond

example:
>>> zz = [
...     ['a', 'b'], ['b', 'c'], ['d', 'e'], 
...     ['f', 'g'], ['h', 'i'], ['j', 'k']
... ]
>>> 
>>> skipped = zz[:4]
>>> skipped
[['a', 'b'], ['b', 'c'], ['d', 'e'], ['f', 'g']]
>>>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace values in Yaml file with value in dictionary PelleH 1 2,283 Feb-11-2025, 09:51 AM
Last Post: alexjordan
  Assigning cycle values in a list nmancini 3 1,040 Sep-16-2024, 09:35 PM
Last Post: deanhystad
  Append from csv to xlsx with values only Sick_Stigma 2 783 Aug-06-2024, 08:05 PM
Last Post: Sick_Stigma
  remove duplicates from dicts with list values wardancer84 27 6,111 May-27-2024, 04:54 PM
Last Post: wardancer84
Question Using Lists as Dictionary Values bfallert 8 2,379 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  Printing out incidence values for Class Object SquderDragon 3 1,320 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 2,094 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  Too much values to unpack actualpy 3 1,537 Feb-11-2024, 05:38 PM
Last Post: deanhystad
  Converting column of values into muliple columns of counts highland44 0 934 Feb-01-2024, 12:48 AM
Last Post: highland44
  __init__() got multiple values for argument 'schema' dawid294 4 9,952 Jan-03-2024, 09:42 AM
Last Post: buran

Forum Jump:

User Panel Messages

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