tags:

views:

391

answers:

6

Hi,

I'm trying to open a file and afterwards create a list with each line read from the file.

   i=0
   List=[""]
   for Line in inFile:
      List[i]=Line.split(",")
      i+=1
   print List

But this sample code give me an error because of the i+=1 saying that index out of range. What's my prblem here? How can I write the code in order to increment my list with every new Line in the InFile?

Thanks for the help

A: 

I am not sure about Python but most languages have push/append function for arrays.

Din
+9  A: 

Its alot easier than that:

List = open("filename.txt").readlines()

This returns a list of each line in the file.

Brian C. Lane
WoooW Just perfect! It worked. Btw imagine that was not a file... But i wnat to creat a dynamic list... How can I append an element on it? like if was an arrayList?ty so much
UcanDoIt
The append method will do the trick. For example: xs.append(line)
Eli Courtwright
+2  A: 
my_list = [line.split(',') for line in open("filename.txt")]
orip
I fear jumping into a list comprehension for someone still trying to understand basic python might be a bit large of a step. :)
Dustin
I suppose you're right...
orip
Dustin is right, however orip offered the most correct answer to the question.
ΤΖΩΤΖΙΟΥ
+1  A: 

A file is almost a list of lines. You can trivially use it in a for loop.

myFile= open( "SomeFile.txt", "r" )
for x in myFile:
    print x
myFile.close()

Or, if you want an actual list of lines, simply create a list from the file.

myFile= open( "SomeFile.txt", "r" )
myLines = list( myFile )
myFile.close()
print len(myLines), myLines

You can't do someList[i] to put a new item at the end of a list. You must do someList.append(i).

Also, never start a simple variable name with an uppercase letter. List confuses folks who know Python.

Also, never use a built-in name as a variable. list is an existing data type, and using it as a variable confuses folks who know Python.

S.Lott
How can I leanr about python coding standards?Thanks, i didn't know you use varaibable names in lowercase... any special reason for that?Ty
UcanDoIt
While you're on conventions, PEP8 also has something to say about spaces inside of parentheses. :)
Dustin
See http://www.python.org/dev/peps/pep-0008/, also see http://stackoverflow.com/questions/159720/what-is-the-naming-convention-in-python-for-variable-and-function-names
S.Lott
@Dustin: well aware of space-in-parenthesis recommendation in PEP8. After 3 decades of spaces in ()'s, I'm not going to change.
S.Lott
What's wrong with spaces inside parentheses? Who cares?
Federico Ramponi
@Federico People who read your code and stop to think about why some parts look different from the rest of the code. :) e.g. what's special about the len() call that makes it consistent with PEP8, but inconsistent with the lines above it? If you don't care, just follow the conventions. :)
Dustin
@Dustin: Yes, trivial spacing inconsistencies are inconsistent. I've seen so much code that simply doesn't work in the first place. I feel that "working" trumps "consistent spacing". If spacing leads to difficulty understanding, then there are larger problems to solve first.
S.Lott
+1  A: 

f.readlines() returns a list that contains each line as an item in the list

if you want eachline to be split(",") you can use list comprehensions

[ list.split(",") for line in file ]
hasen j
+1  A: 

Please read PEP8. You're swaying pretty far from python conventions.

If you want a list of lists of each line split by comma, I'd do this:

l = []
for line in in_file:
    l.append(line.split(','))

You'll get a newline on each record. If you don't want that:

l = []
for line in in_file:
    l.append(line.rstrip().split(','))
Dustin
thanks a lot. very helpfull...
UcanDoIt