Чем readline отличается от readlines в python
Перейти к содержимому

Чем readline отличается от readlines в python

  • автор:

Python readline() and readlines() not working

I’m trying to read the contents of a 5GB file and then sort them and find duplicates. The file is basically just a list of numbers (each on a new line). There are no empty lines or any symbols other than digits. The numbers are all pretty big (at least 6 digits). I am currently using

for line in f: do something to line 

to avoid memory problems. I am fine with using that. However, I am interested to know why readline() and readlines() didn’t work for me. When I try

print f.readline(10) 

the program always returns the same line no matter which number I use as a parameter. To be precise, if I do readline(0) it returns an empty line, even though the first line in the file is a big number. If I try readline(1) it returns 2, even though the number 2 is not in the file. When the parameter is >= 6, it always returns the same number: 291965. Additionally, the readlines() method always returns the same lines no matter what the parameter is. Even if I try to print f.readlines(2), it’s still giving me a list of over 1000 numbers. I am not sure if I explained it very well. Sorry, English is not my first language. Anyway, I can make it work without the readline methods but I really want to know why they don’t work as expected. This is what the first 10 lines of the file look like:

548098 968516 853181 485102 69638 689242 319040 610615 936181 486052 

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *