productner icon indicating copy to clipboard operation
productner copied to clipboard

IndexError while running 'python supplement.py products.normalized.trimmed.csv'

Open Nidhibar opened this issue 4 years ago • 2 comments

Hello etano

I was trying to execute the commands as you have provided. But after trimming and running the following all scripts give the index error. productner-master\data>python supplement.py products.normalized.trimmed.csv Traceback (most recent call last): File "supplement.py", line 15, in title, brand, description = row[0], row[1], row[2] IndexError: list index out of range

Kindly let me know if any other details are needed.

Nidhibar avatar Sep 22 '20 06:09 Nidhibar

I'm facing the same problem. Any solution to this?

Hey guys-- @UTS-ExplainableRecommendation @Nidhibar

Try this fix, worked for me.

`import sys, csv, io

in_file = sys.argv[1] out_file = '.'.join(in_file.split('.')[:-1] + ['supplemented'] + ['csv'])

with io.open(in_file, 'r', encoding='utf-8') as f: reader = csv.reader(f) writer = csv.writer(io.open(out_file,"w", encoding='utf-8')) count, supplemented = 0, 0 for row in reader: count += 1 if not (count % 10000): print (supplemented, '/', count, 'rows supplemented') try: title = row[0] except: title = '' continue try: brand = row[1] except: brand = '' continue try: description = row[2] except: description = '' continue

    if not (brand in title):
        supplemented += 1
        title = brand + ' ' + title
    description = title + ' ' + description
    try:
        title = row[0]
    except:
        title = ''
        continue
    try:
        brand = row[1]
    except:
        brand = ''
        continue
    try:
        description = row[2]
    except:
        description = ''
        continue
    
    writer.writerow(row)
print (supplemented, '/', count, 'rows supplemented')`

jtaylor-dev avatar Jul 08 '21 21:07 jtaylor-dev