make a string a list

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

    make a string a list

    or a string iterable ? How can I do that. I have lots of '\r\n'
    characters in the string which I think can be easier if it were made
    into a list and I can easily see if the required value (its a numeral)
    is present in it or not after some position or after some characters'
    position.

    Thanks,
    Nikhil
  • jay graves

    #2
    Re: make a string a list

    On May 29, 4:30 pm, Nikhil <mnik...@gmail. comwrote:
    or a string iterable ? How can I do that. I have lots of '\r\n'
    characters in the string which I think can be easier if it were made
    into a list and I can easily see if the required value (its a numeral)
    is present in it or not after some position or after some characters'
    position.
    Hmmm. Can you state your problem in a different way?

    I find that Python excels at string handling and I've never had to
    write a pure character scanner like you are suggesting. Not to say
    that you don't need it, I just think that if you give us a small
    example of your data and your expected outcome, it would be easier to
    help you.


    Comment

    • Matimus

      #3
      Re: make a string a list

      On May 29, 2:30 pm, Nikhil <mnik...@gmail. comwrote:
      or a string iterable ? How can I do that. I have lots of '\r\n'
      characters in the string which I think can be easier if it were made
      into a list and I can easily see if the required value (its a numeral)
      is present in it or not after some position or after some characters'
      position.
      >
      Thanks,
      Nikhil
      I a little confused by what you are trying to do, but:

      Strings are iterable.
      >>for c in "hello":
      .... print "look, a letter", c
      ....
      look, a letter h
      look, a letter e
      look, a letter l
      look, a letter l
      look, a letter o

      And indexable:
      >>"hello"[0]
      'h'

      And converting to a list is done like this:
      >>list("hello ")
      ['h', 'e', 'l', 'l', 'o']

      They also have some pretty nifty methods:
      >>"hello\r\nwor ld\r\nlets\r\ns plit\r\n".split ("\r\n")
      ['hello', 'world', 'lets', 'split', '']
      >>"hello world".index('w orld')
      6
      >>"hello world".index('l ')
      2
      >>"hello world".index('l ', 2+1)
      3
      >>"hello world".index('l ', 3+1)
      9
      >>"hello world".index('l ', 9+1)
      Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      ValueError: substring not found

      Hopefully some of that helps.

      Matt

      Comment

      • Ben Finney

        #4
        Re: make a string a list

        Nikhil <mnikhil@gmail. comwrites:
        or a string iterable ? How can I do that. I have lots of '\r\n'
        characters in the string which I think can be easier if it were made
        into a list and I can easily see if the required value (its a numeral)
        is present in it or not after some position or after some characters'
        position.
        What problem are you trying to solve?

        Are you aware that Python file objects are already iterable, yielding
        one line of text per iteration?

        input_file = open("foo.txt")
        for line in input_file:
        do_stuff(line)

        --
        \ "I'd take the awe of understanding over the awe of ignorance |
        `\ any day." -- Douglas Adams |
        _o__) |
        Ben Finney

        Comment

        • Alan Isaac

          #5
          Re: make a string a list

          Nikhil wrote:
          or a string iterable ? How can I do that. I have lots of '\r\n'
          characters in the string which I think can be easier if it were made
          into a list and I can easily see if the required value (its a numeral)
          is present in it or not after some position or after some characters'
          position.
          Why dont the ``find`` or ``index`` methods work for you?


          Cheers,
          Alan Isaac

          Comment

          • iapain

            #6
            Re: make a string a list

            On May 29, 11:30 pm, Nikhil <mnik...@gmail. comwrote:
            or a string iterable ? How can I do that. I have lots of '\r\n'
            characters in the string which I think can be easier if it were made
            into a list and I can easily see if the required value (its a numeral)
            is present in it or not after some position or after some characters'
            position.
            >
            Thanks,
            Nikhil
            If you just want to check required value then you can even use Sets or
            build your own hash table.

            Comment

            • Tobiah

              #7
              Re: make a string a list

              >or a string iterable ? How can I do that. I have lots of '\r\n'
              >characters in the string which I think can be easier if it were made
              >into a list and I can easily see if the required value (its a numeral)
              >is present in it or not after some position or after some characters'
              >position.
              They already are. They are quite like lists in many ways:
              >>s = 'abcdefg'
              >>for c in s: print c
              ....
              a
              b
              c
              d
              e
              f
              g
              >>s[3]
              'd'
              >>s = "foo\r\n"
              >>s.find("\r" )
              3
              >>s.replace("\r ", "").replace("\n ", "")
              'foo'
              >>>
              ** Posted from http://www.teranews.com **

              Comment

              • Lie

                #8
                Re: make a string a list

                On May 30, 4:30 am, Nikhil <mnik...@gmail. comwrote:
                or a string iterable ? How can I do that. I have lots of '\r\n'
                characters in the string which I think can be easier if it were made
                into a list and I can easily see if the required value (its a numeral)
                is present in it or not after some position or after some characters'
                position.
                >
                Thanks,
                Nikhil
                Isn't it already iterable? And combined with str.split(), it could be
                line iterable too.

                Comment

                Working...