I'm having trouble with a relatively simple problem.. I have some data like so in CSV:
period, reading
1, 3
2, 4
3, 5
4, 2
5, 2
I simply want the second column to shift up by one, while the first column stays the same. After the second column shifts up by one, I would like the last value to be replaced with a random value in the range -5, 5 (hopefully you can see this from my script).
import csv
import random
directory = r"C:/Program Files (x86)/CM/data.csv"
with open(directory, 'r') as csvfile:
s = csvfile.readlines()
dataCSV = []
for i, point in enumerate(s[1:]):
seperatedPoint = point.strip("\n").split(",")
if len(seperatedPoint) == 2:
dataCSV.append([int(dataPoint) for dataPoint in seperatedPoint])
l = len(dataCSV)
for i in range(l-1):
dataCSV[i][1] = dataCSV[i+1][1]
dataCSV[l-1][1] += random.randint(-5,5)
with open(directory, 'w') as csvfile: #opens the file for writing
output = csv.writer(csvfile, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
output.writerow(["period", "reading"])
header = (["period", "reading"])
print( ", ".join( str(e) for e in header ) )
print ""
for point in dataCSV:
output.writerow(point)
print( ", ".join( str(e) for e in point ) )
print ""
However instead of shifting up by one, this is just spitting out a the same value a ton of times in the second column.. It will then change values and spit out a ton of those repeated values as well, until I get to the end of my range. I can't figure out what I'm doing wrong. Any help is appreciated, thank you.
You're shifting all the data up every time you add a data point to the list. To see this, add a print line here:
Which will print out the following on your sample input:
The large number of "setting..." lines is your first clue that something is wrong: there should only be four shift operations.
The culprit, in this case, is very simple: the loop that performs the shifting is indented too far. It should be at the same level as the loop that initializes
dataCSV
.Mostly unrelated to the question: you could clean up this code and potentially avoid this kind of mishap by cleaning up your file reading. Rather than reading the whole file, and then manually creating lists and throwing each line into the list in an explicit loop, you'd be better off using the
csv
module to read the file as well as to write it: