0

i would like to read a csv sheet. So far i have a loop which reads all rows from sheet and creates a specific txt file for each row.

with openCSV(file) as newData:
    reader = csv.reader(newData)
    next(reader)
    dictData = {}
    for i, row in enumerate(reader, 1)
        dictData = {'articleID' = row[0], 'Desc':row[1], ....}

thats working fine.

Now what i want is:

If there are more articleID's with the same numbers it should be added in one txt file.

How can i do this?

My CSV File:

enter image description here

balderman
  • 22,927
  • 7
  • 34
  • 52
m1711
  • 101
  • 1
  • 8

2 Answers2

0

I think pandas would be a perfect fit for this job

Install pip3 install pandas

Usage

import pandas as pd

df = pd.read_csv("data.csv")
df[df.duplicated(['ID'], keep=False)] #gives you all the duplicates

keep : {‘first’, ‘last’, False}, default ‘first’

  • first : Mark duplicates as True except for the first occurrence.
  • last : Mark duplicates as True except for the last occurrence.
  • False : Mark all duplicates as True.
Kuldeep Singh Sidhu
  • 3,748
  • 2
  • 12
  • 22
0

Below. The code creates 3 csv files. One for each id.

import csv
from collections import defaultdict

data_by_id = defaultdict(list)
with open('c:\\temp\\temp.csv')as f:
    reader = csv.reader(f)
    next(reader)
    for row in reader:
        data_by_id[row[0]].append(row[1:])

for _id, data in data_by_id.items():
    with open('c:\\temp\\{}.csv'.format(_id), 'w') as f:
        f.write('id,description,price\n')
        for line in data:
            line.insert(0, _id)
            f.write(','.join(line) + '\n')

temp.csv

id,description,price
12,desc1,12.4
13,desc13,13.4
12,desc132,312.4
13,desc1er,5.8
11,desc1wewe,77.9
balderman
  • 22,927
  • 7
  • 34
  • 52
  • i reveive the error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfc in position 5428: invalid start byte – m1711 Jun 29 '20 at 08:28
  • What did you try to do? Please be detailed as much as you can? The code was tested and produced the requested output. – balderman Jun 29 '20 at 08:39
  • Before i modify my code i wanted to try your code i just changed the Path. Just to make sure that your code is working. – m1711 Jun 29 '20 at 08:42
  • See https://repl.it/repls/CultivatedVictoriousMonotone for running code – balderman Jun 29 '20 at 08:49
  • your code is running fine. The error appears when i navigate the path to my sheet.i have nearly 2000 rows and when i compile ii i receive the error UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfc in position 5428: invalid start byte – m1711 Jun 29 '20 at 09:46
  • see here https://stackoverflow.com/questions/19591458/python-reading-from-a-file-and-saving-to-utf-8/19591815 – balderman Jun 29 '20 at 09:51