Re: File Reading related query

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Chase

    Re: File Reading related query

    I am talking about text file which contain multiple lines e.g following
    three lines are there in my textfile.txt
    >
    this is python list
    where we get support
    from geeks
    >
    I want
    >
    sentence = this is python list where we get support from geeks
    >
    whereas when i use simple read() i get something like this
    >
    sentence = this is python list\nwhere we get support\nfrom geeks

    You mean

    sentence = myfile.read().r eplace('\n', ' ')

    ?

    If you want to compact multiple spaces, such as

    this is the python__
    list where__
    __we get support

    (where "_" represents a space) and you want that to become

    this is the python list where we get support

    you'd have to use slightly more intelligent processing, and
    explain the behavior you'd want if more than one blank line was
    encountered. But for a first pass:

    import re
    r = re.compile(r"\s *\n")

    sentence = r.sub(" ", myfile.read())

    -tkc



Working...