python - From file to list -
hello guys trying read file digits put example(below) list[1,2,3,4]
:
1
2
3
4
5
im doing in class. tried without class this:
def velocity(): items = [] open('smhi.txt') input: line in input: items.extend(line.strip().split()) return items
when print items ["1,","2","3","4"]
you need convert numbers int
,and can use list comprehension following but,note define list inside function in local name space couldn't access outside of function!so need return list :
def velocity(): open('smhi.txt') input: my_list=[int(line) line in input if line.strip()] return my_list
result :
[1, 2, 3, 4, 5]
Comments
Post a Comment