问题
I have a bunch of float values, for example:
x1 = 1.11111111
x2 = 2.22222222
I want to write these values to a file:
f = open("a.dat", "w+")
f.write("This is x1: ",x1)
f.write("\n") #I want to separate the 2 lines
f.write("This is x2: ",x2)
At this point I got an error on the second line:
write() takes exactly one argument (2 given)
How do I write to file such that when I open it, I see this format:
This is x1: 1,1111111
This is x2: 2,2222222
And yes, the file has to be ***.dat
It's not .txt
回答1:
the way you are writing to the file looks like you are giving two arguments to write function. So you need to only pass one argument. try converting x1 and x2 into string and then write to the file.
f.write("This is x1 " + str(x1))
f.write("This is x2 " + str(x2))
回答2:
The write function takes a single string. You're trying to use it like print
, which takes any number of arguments.
You can, in fact, just use print. Its output only goes to your program's output (stdout) by default, by passing it a file
argument, you can send it to a text file instead:
print("This is x1: ", x1, file=f)
If you want to use write
, you need to format your output into a single string. The easiest way to do that is to use f-strings:
f.write(f"This is x1: {x1}\n")
Notice that I had to include a \n
on the end. The print
function adds its end
parameter to the end of what it prints, which defaults to \n
. The write
method does not.
Both for backward compatibility and because occasionally they're more convenient, Python has other ways of doing the same thing, including explicit string formatting:
f.write("This is x1: {}\n".format(x1))
… printf-style formatting:
f.write("This is x1: %s\n" % (x1,))
… template strings:
f.write(string.Template("This is $x1\n").substitute(x1=x1))
… and string concatenation:
f.write("This is x1: " + str(x1) + "\n")
All but the last of these automatically converts x1
to a string in the same way as str(x1)
, but also allows other options, like:
f.write(f"This is {x1:.8f}\n")
This converts x1
to a float
, then formats it with 8-decimal precision. So, in addition to printing out 1.11111111
and 2.22222222
with 8 decimals, it'll also print 1.1
as 1.10000000
and 1.23456789012345
as 1.23456789
.
The same format strings work for f-strings, str.format
, and the format
functions:
print("This is x1: ", format(x1, '.8f'), file=f)
f.write("This is x1: {:.8f}\n".format(x1))
f.write("This is x1: " + format(x1, '.8f') + "\n")
… and the other two methods have similar, but not quite as powerful, formatting languages of their own:
f.write("This is x1: %.8f\n" % (x1,))
回答3:
f.write('This is x1: %f'%x1)
f.write('This is x2: %f'%x2)
回答4:
First have a look at the below code sample. I have used repetition operator *
to repeat strings 2 times which can used to generate a multiline string in single statement(in case, if you have a set of variables).
x1 = 1.11111111
x2 = 2.22222222
lines = "This is x%s: %s\n"*2 % (1, x1, 2, x2)
print(lines)
» Output
This is x1: 1.11111111
This is x2: 2.22222222
Finally you can use the below 3 lines of code to accomplish your goal.
x1, x2 = 1.11111111, 2.22222222
with open("a.dat", "w+") as f:
f.write("This is x%s: %s\n"*2 % (1, x1, 2, x2));
Do not need to close file. It will be closed automatically once program control comes out from with statement's block.
来源:https://stackoverflow.com/questions/50893146/what-is-the-correct-format-to-write-float-value-to-file-in-python