0
fruits = ['bananas', 'apples', 'oranges', 'strawberry']
with open("fruits_text.txt", 'w') as totxt_file:
    totxt_file.write(str(fruits)+'\n')

The "\n" in code above doesnt work, when I run it here is what I got

['bananas', 'apples', 'oranges', 'strawberry']

How can I export it without commas and quotes? like this

bananas
apples 
oranges
strawberry 
  • 1
    "The "\n" in code above doesnt work" Yes, it does. It results in a single newline at the end of the file, because you are concatenating a single newline to the end of the string that you get from `str(fruits)`. If you want to write multiple lines, then you need something that puts multiple `'\n`s into the output. The straightforward way to do this is to make multiple `write` calls, one for each item in the list. Do you know what a `for` loop is? – Karl Knechtel Sep 02 '21 at 03:18
  • Another way is to create a single string that has all the list contents and newlines in it. Try putting `python how do I join a list of strings` into a search engine. – Karl Knechtel Sep 02 '21 at 03:19
  • 2
    If you are confused at this point, it's because you are trying to do too much at once. You should first check that you know how to solve the problem with ordinary `print` output, and then study about how to use files. – Karl Knechtel Sep 02 '21 at 03:20

4 Answers4

3

One way using str.join:

fruits = ['bananas', 'apples', 'oranges', 'strawberry']
with open("fruits_text.txt", 'w') as totxt_file:
    totxt_file.write("\n".join(fruits))

Note that this doesn't insert a line separator (\n here) at the very end of the last line, which might be problematic in some cases.

Output:

# cat fruits_text.txt
bananas
apples
oranges
strawberry
Chris
  • 29,127
  • 3
  • 28
  • 51
  • 1
    This has the slight disadvantage of not including a linebreak at the end of the file which can lead to problems. It also violates POSIX which may create some emotional discomfort :) – Jan Wilamowski Sep 02 '21 at 03:22
  • 1
    @JanWilamowski Indeed it doesn't leave the linebreak at the end. I often use it to _avoid_ so I didn't realize it could be disadvantage, let me edit. For the second issue, can you elaborate a bit more :)? – Chris Sep 02 '21 at 03:25
  • 2
    @Chris here you go: https://stackoverflow.com/questions/729692/why-should-text-files-end-with-a-newline – Jan Wilamowski Sep 02 '21 at 03:26
2

Iterate over all items and write them separately:

fruits = ['bananas', 'apples', 'oranges', 'strawberry']
with open("fruits_text.txt", 'w') as totxt_file:
    for fruit in fruits:
        totxt_file.write(fruit + '\n')
Jan Wilamowski
  • 3,308
  • 2
  • 10
  • 23
  • 1
    or replace the `for` loop with `totxt_file.write('\n'.join(fruits))` – bb1 Sep 02 '21 at 03:18
  • @bb1 I'm no a fan of that, as I've explained on the other answer. The only advantage seems to be slightly shorter code but that's offset by more complexity and memory usage, on top of the missing final linebreak. – Jan Wilamowski Sep 02 '21 at 03:31
  • Well then: `totxt_file.write('\n'.join(fruits) + '\n')` – bb1 Sep 02 '21 at 03:35
  • @bb1 now you've alleviated one issue at the expense of making it even less readable while using even more memory. I'm not convinced :) – Jan Wilamowski Sep 02 '21 at 03:37
0

You can unpack the list as arguments for the print function, which supports a file keyword argument for file output. Use the sep keyword argument to separate each record by newlines:

with open("fruits_text.txt", 'w') as totxt_file:
    print(*fruits, sep='\n', file=totxt_file)
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

You could use writelines() with a comprehension to add an end of line to each string:

fruits = ['bananas', 'apples', 'oranges', 'strawberry']
with open("fruits_text.txt", 'w') as totxt_file:
    totxt_file.writelines(f+"\n" for f in fruits)
Alain T.
  • 40,517
  • 4
  • 31
  • 51