Python: readlines issue

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

  1. macrumors 6502

    I'm using readlines, and if I do
    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

    Feb 2, 2008
    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

    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

    Feb 2, 2008
    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

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

Share This Page