I am new to Python and trying to essentially do the following four things
- Pull numerical data (likely floats) from a csv
- Turn those floats into variables
- Perform an operation (multiplication, addition, subtraction, etc.) on those variables
- Output those variables to a new list
I am attempting to perform multiplication in the code below, but instead it ends up just printing the value 3 times.
import csv
with open('pull_vars.csv', 'r+') as pullfile:
reader = csv.reader(pullfile)
mydict = {rows[0]:rows[1] for rows in reader}
for rows in mydict:
mydict[rows] *= 3
print(mydict)
My input
{'1': ' 1', '2': ' 2', '3': ' 3', '4': ' 4', '5': ' 5', '6': ' 6', '7': ' 7', '8': ' 8', '9': ' 9'}
My output ends up looking like this
{'1': ' 1 1 1', '2': ' 2 2 2', '3': ' 3 3 3', '4': ' 4 4 4', '5': ' 5 5 5', '6': ' 6 6 6', '7': ' 7 7 7', '8': ' 8 8 8', '9': ' 9 9 9'}
How can I perform the operation on the value (as a number) versus as a string? Thanks for any help! EDIT 1: The operation I want to do is multiply all values in my dictionary (generated via CSV) by 3.
EDIT 2: before, I showed the dictionary created from my csv input, rather than the csv input itself. My CSV input is.
1, 1
2, 2
3, 3
4, 4
5, 5
6, 6
7, 7
8, 8
9, 9