adding a row to CSV file - TypeError: must be str, not tuple

后端 未结 1 538
孤城傲影
孤城傲影 2021-01-21 04:36

Trying to add a row to my CSV file in the format; \"name, value\". Here is the CSV file:

Japanese Yen,169.948
US Dollar,1.67
Pound Sterling,1
Euro,5.5

相关标签:
1条回答
  • 2021-01-21 05:02

    Well, do some testing before asking. Before file.write(adding) you could have put a print(adding) of print(adding.__repr__()) to see what is Pythons problem with your adding instance. You would have seen it is like ('Euro', '5.5') which is a tuple. True if Python complains it needs a string instead of a tuple you passed there a tuple. First, don't convert to float the newRt input since you need it as a string either. What you need here is adding = ','.join((addCurrency, newRt)) + '\n' or adding = '{},{}\n'.format(addCurrency, newRt) or adding = addCurrency + ',' + newRt + '\n'. Try to figure out simple problems like this one before questioning SO. Read the tutorial of the documentation.

    0 讨论(0)
提交回复
热议问题