4

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

4
  • Shouldn't it be f.write("\n")? With quotes.
    – dcg
    Jun 17, 2018 at 2:16
  • oh yes, I fixed that too. But the program still won't compile, as its stop right at the 2nd line
    – Tamamo
    Jun 17, 2018 at 2:17
  • 4
    write doesn't behave like print, it will only take a single argument, you are passing more than one. The error is pretty self-explanatory here. Jun 17, 2018 at 2:17
  • @user3483203 that explains it. Can you post a comment with links that help me formatting it? So I can pick your comment as an answer and close this thread.
    – Tamamo
    Jun 17, 2018 at 2:19

4 Answers 4

12

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
  • I'm afraid I haven't learn anything about stdout yet. I'll try write this down and use it next time.
    – Tamamo
    Jun 17, 2018 at 2:27
  • 2
    @AdrianTran stdout is just the name for the special file that represents your program's output. You don't hav to worry about what it means; just that you can use a normal file that you got back from open with the print function.
    – abarnert
    Jun 17, 2018 at 2:34
  • @AdrianTran stdout is just the place where normal print output goes. That usually means your terminal (console) window.
    – PM 2Ring
    Jun 17, 2018 at 2:37
5

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))
1
  • 4
    @AdrianTran Note that this code writes everything on one line. And using str to convert floats doesn't give you any format control, you just get the default number of decimal places.
    – PM 2Ring
    Jun 17, 2018 at 2:35
1
f.write('This is x1: %f'%x1)
f.write('This is x2: %f'%x2) 
1
  • tks! btw can you find any website that can show me how to format correctly when write to file?
    – Tamamo
    Jun 17, 2018 at 2:25
1

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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.