Profile

Click to view full profile
Hi, I'm Veerapat Sriarunrungrueang, an expert in technology field, especially full stack web development and performance testing.This is my coding diary. I usually develop and keep code snippets or some tricks, and update to this diary when I have time. Nowadays, I've been giving counsel to many well-known firms in Thailand.
view more...

Tuesday, March 22, 2011

How to read line from an input file without \n

In python, when you read each line from a file, the line string will contain \n at the end of line (1 character).
So, you have to remove it manually unlike Java you can read line in Scanner class.
f = open(...)               # Open file

f.readline().rstrip('\n')   # Remove last character
or
f.readline()[:-1]           # Remove last character

No comments:

Post a Comment