How to convert " " in a string to blank space?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • 一首诗

    How to convert " " in a string to blank space?

    Is there any simple way to solve this problem?

  • martdi

    #2
    Re: How to convert "&nbsp ;" in a string to blank space?


    一首诗 wrote:
    Is there any simple way to solve this problem?
    >>myString = " "
    >>myString = myString.replac e(" ", "")

    Comment

    • wittempj@hotmail.com

      #3
      Re: How to convert "&nbsp ;" in a string to blank space?

      Is this what you want?

      pys = 'This string contains   two times   - end'
      pyprint s.replace('&nbs p;', ' '*6)
      This string contains two times - end


      see http://docs.python.org/lib/string-methods.html

      On Oct 30, 6:26 pm, "一首诗" <newpt...@gmail .comwrote:
      Is there any simple way to solve this problem?

      Comment

      • Gary Herron

        #4
        Re: How to convert &quot;&amp;nbsp ;&quot; in a string to blank space?

        一首诗 wrote:
        Is there any simple way to solve this problem?
        >
        Yes, strings have a replace method:
        >>s = "abc&nbsp;d ef"
        >>s.replace('&n bsp;',' ')
        'abc def'

        Also various modules that are meant to deal with web and xml and such
        have functions to do such operations.


        Gary Herron



        Comment

        • 一首诗

          #5
          Re: How to convert &quot;&amp;nbsp ;&quot; in a string to blank space?

          Oh, I didn't make myself clear.

          What I mean is how to convert a piece of html to plain text bu keep as
          much format as possible.

          Such as convert "&nbsp;" to blank space and convert <brto "\r\n"

          Gary Herron wrote:
          一首诗 wrote:
          Is there any simple way to solve this problem?
          Yes, strings have a replace method:
          >
          >s = "abc&nbsp;d ef"
          >s.replace('&nb sp;',' ')
          'abc def'
          >
          Also various modules that are meant to deal with web and xml and such
          have functions to do such operations.


          Gary Herron

          Comment

          • wittempj@hotmail.com

            #6
            Re: How to convert &quot;&amp;nbsp ;&quot; in a string to blank space?



            On Oct 30, 6:44 pm, "一首诗" <newpt...@gmail .comwrote:
            Oh, I didn't make myself clear.
            >
            What I mean is how to convert a piece of html to plain text bu keep as
            much format as possible.
            >
            Such as convert "&nbsp;" to blank space and convert <brto "\r\n"
            >
            Then you can explore the parser,
            http://docs.python.org/lib/module-HTMLParser.html, like

            #!/usr/bin/env python
            from HTMLParser import HTMLParser

            parsedtext = ''

            class Parser(HTMLPars er):
            def handle_starttag (self, tag, attrs):
            if tag == 'br':
            global parsedtext
            parsedtext += '\\r\\n'

            def handle_data(sel f, data):
            global parsedtext
            parsedtext += data

            def handle_entityre f(self, name):
            if name == 'nbsp':
            pass

            x = Parser()
            x.feed('An &nbsp; text<br>')
            print parsedtext

            Gary Herron wrote:
            一首诗 wrote:
            Is there any simple way to solve this problem?
            >
            Yes, strings have a replace method:
            >
            >>s = "abc&nbsp;d ef"
            >>s.replace('&n bsp;',' ')
            'abc def'
            >
            Also various modules that are meant to deal with web and xml and such
            have functions to do such operations.
            Gary Herron

            Comment

            • Fredrik Lundh

              #7
              Re: How to convert &quot;&amp;nbsp ;&quot; in a string to blank space?

              一首诗 wrote:
              Is there any simple way to solve this problem?
              &nbsp; corresponds to a non-breaking space, chr(160). if you're only
              dealing with this specific XML/HTML entity, you can do

              text = text.replace("& nbsp;", " ")

              or

              text = text.replace("& nbsp;", chr(160))

              to handle arbitrary entities and character references, pass the data
              through an HTML or XML parser, or use something like:



              </F>

              Comment

              • Frederic Rentsch

                #8
                Re: How to convert &quot;&amp;nbsp ;&quot; in a string to blank space?

                一首诗 wrote:
                Oh, I didn't make myself clear.
                >
                What I mean is how to convert a piece of html to plain text bu keep as
                much format as possible.
                >
                Such as convert "&nbsp;" to blank space and convert <brto "\r\n"
                >
                Gary Herron wrote:
                >
                >一首诗 wrote:
                >>
                >>Is there any simple way to solve this problem?
                >>>
                >>>
                >>>
                >Yes, strings have a replace method:
                >>
                >>
                >>>>s = "abc&nbsp;d ef"
                >>>>s.replace(' &nbsp;',' ')
                >>>>>
                >'abc def'
                >>
                >Also various modules that are meant to deal with web and xml and such
                >have functions to do such operations.
                >>
                >>
                >Gary Herron
                >>
                >
                >
                >>my_translatio ns = '''
                "&nbsp;= "
                # "<br>=\r\n" "<BR>=\r\n" # Windows
                "<br>=\n" "<BR>=\n" # Linux
                # Add others to your heart's content
                '''
                >>My_Translat or = SE.SE (my_translation s)
                >>print My_Translator ('ABC&nbsp;DEFG <br>XYZ')
                ABC DEFG
                XYZ

                SE can also strip tags and translate all HTM escapes and generally lets
                you do ad hoc translations in seconds. You just write them up, make an
                SE object from your text an run your data through it. As simple as that.
                If you wish further explanations, I'll be happy to explain.

                Frederic


                Comment

                Working...