0

Is there a way to write a long string of words to a text file in python3 so that the string can be written over several lines? This is what I have tried so far.

Here is what I am trying to do

    from nltk.tokenize import word_tokenize

    my_list = word_tokenize(my_text) 
    my_list_string = ' '.join(my_list)

    outfile = open("my_comp.txt", "a") 
    outfile.write(my_list_string ) 
    outfile.close() 
  • Your title doesn't really match your description – karina Apr 23 '16 at 05:02
  • I think this is what you are looking for http://stackoverflow.com/questions/10660435/pythonic-way-to-create-a-long-multi-line-string – karina Apr 23 '16 at 05:04
  • What do you mean by "string can be written over several lines"? – EbraHim Apr 23 '16 at 05:10
  • @karina Thanks, but I am not sure it works. I am getting some data from the web which I tokenize into words and which I would like to save in a text file by writing to it. So I need to split the string up so the text file is readable. – Shubha Rajopadhye Apr 23 '16 at 05:11
  • I think you need to show example input, output, and the code you have tried – karina Apr 23 '16 at 05:12
  • You can split the string on every `.` character and store the result to a list. And then write those elements of your list in your file. Right? – EbraHim Apr 23 '16 at 05:32
  • @EbraHim, I am not sure how I would do that, since my string is of variable length – Shubha Rajopadhye Apr 23 '16 at 05:50

2 Answers2

1

There is a library for that in Python named textwrap:

>>> import textwrap
>>> strs = "Is there a way to write a long string of words to a text file in python3 so that the string can be written over several lines? This is what I have tried so far. Is there a way to write a long string of words to a text file in python3 so that the string can be written over several lines? This is what I have tried so far. "

Works as below:

>>> print (textwrap.fill(strs, 20))
Is there a way to
write a long string
of words to a text
file in python3 so
that the string can
be written over
several lines? This
is what I have tried
so far. Is there a
way to write a long
string of words to a
text file in python3
so that the string
can be written over
several lines? This
is what I have tried
so far.

Or change the line length:

>>> print (textwrap.fill(strs, 40))
Is there a way to write a long string of
words to a text file in python3 so that
the string can be written over several
lines? This is what I have tried so far.
Is there a way to write a long string of
words to a text file in python3 so that
the string can be written over several
lines? This is what I have tried so far.
>>> 
EbraHim
  • 2,279
  • 2
  • 16
  • 28
  • Thank you! this works perfectly! Is there a similar command for a string of numbers? It does not seem to work even after converting the numbers to string. – Shubha Rajopadhye Apr 24 '16 at 14:36
0

After seeing your code I'm not sure there is any other way then to insert a new line every nth token

But who knows how big n should be...

You should probably just leave it up to the program that is opening the file?

karina
  • 805
  • 5
  • 10