#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0

    Smile Two questions from a Python beginner


    Hi,

    firstly,
    why does 'i = str(54832||34567)' give syntaxError for the second '|' ?
    Secondly, when I tried:
    >>> i = str(54832|34567)
    >>>i.split('|')
    ['55095']
    where was this '55095' from?


    Thanks in advance for answering my questions.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    I'm not sure what you are trying to accomplish, please elaborate on your desired output.

    What happens is that you're performing a "bitwise or", (see http://wiki.python.org/moin/BitwiseOperators) like this:

    Code:
    >>> 54832 | 34567
    55095
    Then, by using str(), you take 55095 and make it into a string.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    thanks for the quick response.
    Sorry for not clearly presenting my questions.

    I have a file with a column like '54832||34567'. For this column, I would like to split it by '||' and have an output of list of string, like '54832' and '34567'.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by claudiaH
    thanks for the quick response.
    Sorry for not clearly presenting my questions.

    I have a file with a column like '54832||34567'. For this column, I would like to split it by '||' and have an output of list of string, like '54832' and '34567'.
    Ah.

    Code:
    >>> a = '54832||34567'
    >>> a.split('||')
    ['54832', '34567']
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    Thank you very much. But my original code is for reading the lines from a file. For each line,

    line_list = line.split()
    next_key = str(line_list[6]) ## this column is 54832||34567
    key_list = next_key.split('||')

    How can I make the column a string before I use 'split' if I don't use 'str()'?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by claudiaH
    Thank you very much. But my original code is for reading the lines from a file. For each line,

    line_list = line.split()
    next_key = str(line_list[6]) ## this column is 54832||34567
    key_list = next_key.split('||')

    How can I make the column a string before I use 'split' if I don't use 'str()'?
    When you split() a string, you get a list of new strings.

    If you have problems with your complete code, please paste it in full and we'll help you debug it.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    4
    Rep Power
    0
    the code works now. Thank you for your help.

IMN logo majestic logo threadwatch logo seochat tools logo