Question! I'm going to write a list element in the file

Asked 1 years ago, Updated 1 years ago, 82 views

You are about to write a list element to a file. For example,

#PythonFile
mylist = ["hello", "3"]

file = open("myfile.txt", "w")
file.writelines(["%s\n" % item for item in mylist])

When you run myfile.txt

hello
3

Let's come out together.

My cord is working well right now, but I'm not sure if I'm using it well.

list file file-io python newline

2022-09-22 22:14

1 Answers

Usually

for item in thelist:
    thefile.write("%s\n" % item)

#or

for item in thelist:
    print>>thefile, item

It's written as. If you're going to stick to one line, I think it's better to take out [].


2022-09-22 22:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.