Thread: New problem

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

    Join Date
    Aug 2011
    Posts
    139
    Rep Power
    4

    New problem


    this is an example of my line:

    20978 00:00:51 G 24120 CBST

    when i line.split()

    i get:

    [u'27500', u'00:00:51', u'G', u'.....]

    what's up with the u's? they cause an error when i use:

    line.split()[3] gives an index out of range error

    i can get rid of the u's with about 3 more lines of code, but it seems bulky. is there a better way to get rid of the u's?

    thanks
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    The "u"s are indicating that the string is interpreted as unicode, as opposed to ASCII. It has nothing to do with "split()" but rather how the string was defined, or read, or conjured in the first place. ---That raises the question: how was it?
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    run your program in python3. The "u"'s are illegal.

    Comments on this post

    • WynnDeezl agrees
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    139
    Rep Power
    4
    i was using:
    Code:
        AVAR=io.open(r'U:\Private\Python\Dumps\AVAR.csv', mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True).readlines()
    changed it to:
    Code:
        AVAR=open(r'U:\Private\Python\Dumps\AVAR.csv','r').readlines()
    and the u's are gone, but there is still a problem.

    for example, here is a sample "Line" of my data:
    20978 00:00:51 G 24120 CBST

    i perform this on it: Line.split()[2] (or any index value), and i get index out of range error this:

    if i do print Line.split() on a command line in Idle, i get:
    ['20978', '00:00:51', 'G', '24120', 'CBST']

    and, as expected, if i do a Line.split()[3] in Idle i get:
    '24120'

    why does it work on Idle command line but not while running?

    thanks
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    I don't know, but are you sure the input file is being read properly?

    You should try to narrow down the bug by producing a minimal code example & input file that illustrates the problem.
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    I suspect the error is not on the input line you think it is, but rather on a blank line, probably at the end of the file. Trap the error with something like "if len(Line)>0" or "if Line != """ or something like that.

    Comments on this post

    • WynnDeezl agrees : you nailed it!!!
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,996
    Rep Power
    481
    And you should demonstrate that idle runs the same version of python as "from the command line"
    import sys;sys.version
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    139
    Rep Power
    4

    blank line


    after RRASHKIN stated that about a blank line, i noticed that when i was testing in Idle, the print would always look like:

    []
    ['asfad, 'afad', 'asdfasdf', '.....]

    rather than:
    ['asfad, 'afad', 'asdfasdf', '.....]
    as it should.

    i added:
    Code:
    if Line != ""
    and the error went away. i guess there was some control character issue, although i was doing a Line.strip() before doing anything with the Line.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    139
    Rep Power
    4

    b49P23TIvg


    when i said command line, i was referring to the Idle line:

    '2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]'

IMN logo majestic logo threadwatch logo seochat tools logo