Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For Loop over a list in Python

Tags:

python

loops

list

I have a train_file.txt which has 3 columns on each row.

For example;

1 10 1

1 12 1

2 64 2

6 17 1

...

I am reading this txt file with

train_data = open("train_file.txt", 'r').readlines()

Then I am trying to get each value with for loop

for eachline in train_data:
    uid, lid, x = eachline.strip().split()

Question: Train data is a huge file that's why I want to just get the first 1000 rows.

I was trying to execute the following code but I am getting an error ('list' object cannot be interpreted as an integer)

for eachline in range(train_data,1000)
        uid, lid, x = eachline.strip().split()
like image 879
drorhun Avatar asked Sep 19 '26 17:09

drorhun


2 Answers

It is not necessary to read the entire file at all. You could use enumerate on the file directly and break early or use itertools.islice:

from itertools import islice

train_data = list(islice(open("train_file.txt", 'r'), 1000))

You can also keep using the same file handle to read more data later:

f = open("train_file.txt", 'r')
train_data = list(islice(f, 1000)) # reads first 1000
test_data = list(islice(f, 100))   # reads next 100
like image 192
user2390182 Avatar answered Sep 21 '26 07:09

user2390182


Maybe try changing this line:

train_data = open("train_file.txt", 'r').readlines()

To:

train_data = open("train_file.txt", 'r').readlines()[:1000]
like image 23
U12-Forward Avatar answered Sep 21 '26 06:09

U12-Forward



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!