This time, we bring you python. How to write variables in the TXT format, python to write variables in the TXT format of the note, the following is the actual case, take a look.
Let's look at a simple example: write a variable into the txt text
f = open (' E:/test.txt ', ' W ') f.write (' Hello world! ') OUT[3]: F.close ()
Results
So how do you write a variable on a row?
In the ' W ' write mode, the next time we write a variable, it overwrites the contents of the original TXT file, which is definitely not what we want. TXT has an append mode ' a ' that enables multiple writes:
f = open (' E:/test.txt ', ' a ') f.write (' The second writing ... ') out[6]: F.close ()
Results
If you want to write by line, we only need to add the line break ' \ n ' at the beginning or end of the string:
f = open (' E:/test.txt ', ' a ') f.write (' \nthe third writing ... ') out[9]: F.close ()
Results
If you want to write multiple variables to a row at the same time, you can use the Writelines () function:
f = open (' E:/test.txt ', ' a ') f.writelines ([' \nthe fourth writing ... ', ', ', ' good ']) F.close ()
Results
Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!
Recommended reading:
How does python achieve the Markov distance
How Python calls MySQL to update data