interwebjill
interwebjill

Reputation: 950

Python: replacing some rows of a csv but not others

I am trying to create a new csv file from an original. The new csv file should be a copy of the old, with the exception that a range of values in one column is multiplied by a constant. The values to alter occur from rows i to j inclusive. Here is the code I am attempting:

import csv
import itertools

i, j = 2, 10785

infile = open('../../combined_kW.csv', 'r')
outfile = open('../../combined_kW_adj.csv', 'w')
reader = csv.reader(infile, delimiter= ',')
datawriter = csv.writer(outfile, delimiter=',')

datawriter.writerow(['date', 'PVkW', 'TBLkW'])
next(reader)    # there is a header row

for row in reader:
    for row in itertools.islice(reader, i, j):
        row[1] = row[1].replace(row[1], str(float(row[1]) * 5))
    datawriter.writerow((row[0], row[1], row[2]))

From a csv with roughly 25,000 rows, the contents of the returned file are only:

date,PVkW,TBLkW
2016/04/04 03:00,0.0,207.23748999999998
2017/07/19 09:00,2921.5,287.15625
2018/01/12 18:00,0.0,267.9414

None of which are related to the rows i and j designated above. How can I better go about this?

Upvotes: 0

Views: 33

Answers (1)

pstatix
pstatix

Reputation: 3848

import csv

i, j = 2, 10785

# assuming Python 3; otherwise omit 'newline'
with open('../../combined_kW.csv', 'r', newline='') as f:
    r = csv.reader(f, delimiter=',')
    rows = list(r)

# slice creates a shallow copy
# meaning each element still points to the same array element in rows!
for row in rows[i:j]:
    row[1] = row[1].replace(row[1], str(float(row[1]) * 5))

with open('../../combined_kW_adj.csv', 'w', newline='') as f:
    w = csv.writer(f, delimiter=',')
    w.writerows(rows)

Upvotes: 1

Related Questions