Python 3.5 convert values during re-write to new csv -
i have csv file writing file, in process want change values based on greater or less than. example, in column in new file (row 2 old one) want transform data number word, depending on value in column. actual data has more columns, , thousands of lines, not exceeding 10k lines. example below:
input.csv duck,35,35 car,100,502 baseball,200,950 gun,500,495 taco,300,300 guitar,100,700 barbie, 200,25 gum,300,19 desired output.csv duck,35,35,order car,100,502,order next month baseball,200,950,no order necessary gun,500,495,order next month taco,300,300,order next month guitar,100,700,order next month barbie, 200,25,order urgent gum,300,19,order urgent
this code far, i'm having trouble conversion of amount new value. think need use enumerate, haven't found examples in research of csv conversion method. please assist.
import csv open('interim results.csv', 'r') source: rdr = csv.reader(source) open('interim results fixed.csv', 'w', newline = '') result: wtr = csv.writer(result) r in rdr: wtr.writerow( ( r[0], r[1], r[2] ) )
edit: taking anonymous' advice, came below code. problem somewhere in "if" / "elif" statments not able correct output. output currenlty not correct, listed below. please assist.
import csv code0 = '25' code1 = '50' code2 = '500' code3 = '900' open('input.csv', 'r') source, open('output.csv', 'w') result: reader = csv.reader(source) writer = csv.writer(result) row in reader: val = row[2] if val <= code0: # below 25 order urgent writer.writerow( ( row[0], row[1], row[2], 'order urgent')) elif val <= code1: # below 50 order writer.writerow( ( row[0], row[1], row[2], 'order now')) elif val <= code2: # below 500 order next month writer.writerow( ( row[0], row[1], row[2], 'order next month')) elif val < code3: # below 900 order next month writer.writerow( ( row[0], row[1], row[2], 'order next month')) elif val >= code3: #over 900 no order necessary writer.writerow( ( row[0], row[1], row[2], 'no order necessary')) output.csv duck,35,35,order car,100,502,order next month baseball,200,950,no order necessary gun,500,495,order (this wrong, should next month) taco,300,300,order (this wrong, should next month) guitar,100,700,order next month barbie, 200,25,order urgent gum,300,19,order urgent
edit: took route , solved problem anonymou's guidance. please see below worked me: had abandon greater , go ranges. had remove "val" make work right.
import csv open 'input.csv', 'r') source rdr = csv.reader(source) open ('output.csv', 'w', newline = '') result: wtr = csv.writer(result) r in rdr: if int (r[2]) in range(0,36): wtr.writerow ( ( r[0], r[1], 'order urgent' ) ) elif int(r[2]) in range(36,51): wtr.writerow ( ( r[0], r[1], 'order now' ) ) elif int(r[2]) in range(51,501): wtr.writerow ( ( r[0], r[1], 'order next month' ) ) elif int(r[2]) in range(501,999): wtr.writerow ( ( r[0], r[1], 'no need order' ) )
you need have function or process data (3rd column). here's way so:
import csv # expected codes , replacements codes = { '50': 'order now', '999': 'no order necessary', '500': 'order next month', 'amount available': 'order timeline' } # can multiple statements in 1 open('input.csv', 'r') fp_in, open('output.csv', 'w') fp_out: reader = csv.reader(fp_in) writer = csv.writer(fp_out) row in reader: val = row[2].strip() # deal weird spaces val = codes.get(val, val) # see if known constant, , replace if exists row[2] = ' ' + val # add space , insert row writer.writerow(row)
i had add code keep weird spacing file contains. adjust needed.
Comments
Post a Comment