Python: readlines issue

Discussion in 'Mac Programming' started by lynkynpark86, Dec 9, 2011.

  1. macrumors 6502

    #1
    I'm using readlines, and if I do
    Code:
    print 'Loading %s...' % (stringFromReadlines)
    It prints this:
    Loading TheTextLoadedFromFile
    ...

    Notice the ... is on a new line. Why is this happening? It's like the string contains a line break.
     
  2. macrumors 68040

    Joined:
    Feb 2, 2008
    #2
    That is probably the case then, a line is ended with a newline symbol. At least the readline() includes the newline.
     
  3. thread starter macrumors 6502

    #3
    So how do I avoid this? I need to have multiple lines in my file, and this only happens on the first line. How could I fix this?
     
  4. macrumors 68040

    Joined:
    Feb 2, 2008
    #4
    I don't know, strip the new line manually? Perhaps there is a more sensible way of solving it with Python, look at the docs.
     
  5. thread starter macrumors 6502

    #5
    Figured it out! Not sure how clean it will be, but
    Code:
    theString = theString.replace('\n','')
    seems to work fine. Thanks for the help!
     

Share This Page