0

I am trying to write mean,var,std to a .txt file but I am getting an error.

import csv
mean = 50.00001075309713
var = 4.120598729047652
std = 2.0299257939756448

with open('Radius.txt', 'w+') as f: 
    writer = csv.writer(f)
    print("Mean =",mean)
    print("var =",var)
    print("std =",std)
    writer.writerow(mean)
    writer.writerow(var)
    writer.writerow(std)

The error is

in <module>
    writer.writerow(mean)

Error: iterable expected, not float

The expected output is

mean = 50.00001075309713
var = 4.120598729047652
std = 2.0299257939756448
4
  • We can see from the docs that: "A row must be an iterable of strings or numbers". A float is not iterable, so you need to wrap it in an iterable container like a list or a tuple. Commented Sep 7, 2022 at 15:05
  • 1
    Does this answer your question? How to fix "Error: iterable expected, not int" when writing to a CSV file? Commented Sep 7, 2022 at 15:06
  • Do you need the csv format or are you happy with a txt file containing the 3 lines from your expected output? Commented Sep 7, 2022 at 15:22
  • CSV format is not mandatory. The expected output in a txt file will do. Commented Sep 7, 2022 at 15:23

2 Answers 2

2

Change:

writer.writerow(mean)
writer.writerow(var)
writer.writerow(std)

to:

writer.writerow((mean, var, std))

The requirement for an iterable means whatever you pass to writerow must be something which can be looped through. The fix here is to put your values into a tuple, the inner bracketed values. It works because tuples can be looped through. 👍

To write the values as a single line with variable prefixes (OP's comment) use:

writer.writerow((f'{mean = }  {var = }  {std = }', ))
Sign up to request clarification or add additional context in comments.

6 Comments

I have now included the expected output. How can writer.writerow((mean, var, std)) be modified?
Modify any of the values mean, var and std by assigning their values: var = 123... Each new call to writerow would create a new tuple from the modified values. A tuple itself cannot be modified, but that doesn't affect our use case here because we are creating a new tuple purely for writerow. The tuple doesn't exist after that but your values still do in their respective named variables mean, var and std.
I mean how do I save values in this format: mean = 50.00001075309713 var = 4.120598729047652 std = 2.0299257939756448 as shown in the expected output.
see updated reply
With the updated reply, it is saving as m,e,a,n, ,=, ,5,0,.,0,0,0,0,1,0,7,5,3,0,9,7,1,3, ,v,a,r, ,=, ,4,.,1,2,0,5,9,8,7,2,9,0,4,7,6,5,2, ,s,t,d, ,=, ,2,.,0,2,9,9,2,5,7,9,3,9,7,5,6,4,4,8 which is not the expected output.
|
2

Based on the clarification from the comments a way without using csv but just a txt file:

mean = 50.00001075309713
var = 4.120598729047652
std = 2.0299257939756448

with open('Radius.txt', 'w+') as f: 
    f.write(f"mean = {str(mean)}\n")
    f.write(f"var = {str(var)}\n")
    f.write(f"std = {str(std)}\n")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.