-   Programming (
-   -   Python newbie - a better way to write some code. (

Getafix 06-24-2009 06:06 PM

Python newbie - a better way to write some code.

I have a question regarding "the python way" to do stuff.
The goal is simple.

1) Open a file
2) Parse it's content
3) split it to a list and move forward.

The file is URL list, really straightforward:

The code is like this:

ulist = open('url.txt', 'r')
lines =
lines = lines.split('\n')

# for some reason there is a newline as a last item of the list, so i have to
lines = lines.remove('')

How can i do it better?

bannock 06-24-2009 06:11 PM

Well usually you want to do something with each line, like so:


for line in open('url.txt'):

bgeddy 06-25-2009 09:22 PM

How about a terse version :

lines=[line.strip() for line in open("url.txt") if line!="\n"]

All times are GMT -5. The time now is 12:38 AM.