i have file , when open it, prints out paragraphs. need join these paragraphs space form 1 big body of text.
for e.g.
for data in open('file.txt'): print data
has output this:
hello name blah. name? hello name blah. name?
how can output this?:
hello name blah. name? hello name blah. name?
i've tried replacing newlines space so:
for data in open('file.txt'): updateddata = data.replace('\n',' ')
but gets rid of empty lines, doesn't join paragraphs
and tried joining so:
for data in open('file.txt'): joineddata = " ".join(data)
but separates each character space, while not getting rid of paragraph format either.
you use str.join
:
with open('file.txt') f: print " ".join(line.strip() line in f)
line.strip()
remove types of whitespaces both ends of line. can use line.rstrip("\n")
remove trailing "\n"
.
if file.txt
contains:
hello name blah. name? hello name blah. name?
then output be:
hello name blah. name? hello name blah. name?
Comments
Post a Comment