-1

I am new to Python and trying to essentially do the following four things

  1. Pull numerical data (likely floats) from a csv
  2. Turn those floats into variables
  3. Perform an operation (multiplication, addition, subtraction, etc.) on those variables
  4. 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
stem__bar
  • 19
  • 4

1 Answers1

0

You have to convert the string to int in order to multiply and not do repeat the strings. Given your inout this should work

mydict[rows] = int(mydict[rows]) * 3

mad_
  • 8,121
  • 2
  • 25
  • 40