Python file object iterable

Is file object in python an iterable

Solution 2:Yes, file objects are iterators. File objects are iterators.

Is file object in python an iterable

this is 1st line this is 2nd line this is 3rd line 
lines = open("test.txt", 'r') for line in lines: print "loop 1:"+line for line in lines: print "loop 2:"+line 
loop 1:this is 1st line loop 1:this is 2nd line loop 1:this is 3rd line 

It doesn’t print loop2 at all.

  1. the file object returned by open(), is it an iterable? that’s why it can be used in a for loop?
  2. why loop2 doesn’t get printed at all?

It is not only an iterable , it is an iterator , which is why it can only traverse the file once. You may reset the file cursor with .seek(0) as many have suggested but you should, in most cases, only iterate a file once.

Yes, file objects are iterators.

Like all iterators, you can only loop over them once , after which the iterator is exhausted. Your file read pointer is at the end of the file. Re-open the file, or use .seek(0) to rewind the file pointer if you need to loop again.

Alternatively, try to avoid looping over a file twice; extract what you need into another datastructure (list, dictionary, set, heap, etc.) during the first loop.

Читайте также:  How to get map key in java

Yes, file objects are iterables but to go back to the beginning of the file you need to use lines.seek(0) , since after the first loop you are at the end of the file.

Your are already at the end of the file. File objects are iterators. Once you iterate over them your are at the final position. Iterating again won’t start from the beginning. If you would like to start at line one again, you need to use lines.seek(0) .

Iterating over lines in a file python, I have seen these two ways to process a file: file = open(«file.txt») for line in file: #do something file = open(«file.txt») contents = file.read() for line in contents: # do something I know that in the first case, the file will act like a list, so the for loop iterates over the file as if Code samplewith open(‘file.txt’,’r’) as fin:lines = fin.readlines()for line in lines:# do somethingFeedback

How do I make the «for» loop cycle through all lines of a text file in python 3.8?

I have a text file «abc.txt» containing several lines of text. I am trying to count and list the frequency of appearance of each letter of the alphabet, a till z, in the txt file.

In the code below, after checking for the letter ‘a’ correctly, it returns 0 as count for the rest of the alphabets from ‘b’ onwards. Once the ‘for’ loop is executed for the letter ‘a’, for the subsequent letters, how I make it loop back to the beginning of the text again?

with open(r"C:\Users\username\Downloads\abc.txt","r") as x: for j in "abcdefghijklmnopqrstuvwxyz": n = 0 for i in x: y = i.count(j) n += y print(n) 

This is because once you’ve read the entire file, the «pointer» or «cursor» is now at the end of the file, there are no more lines to read, and thus the rest of the checks end with 0.

To fix this move the cursor back to the top of the file after each loop of the outer for loop using the seek() function:

import os with open(r"C:\Users\username\Downloads\abc.txt","r") as x: for j in "abcdefghijklmnopqrstuvwxyz": n = 0 for i in x: y = i.count(j) n += y print(n) x.seek(0, os.SEEK_SET) 

It is probably worth making everyline lowercase before counting:

Your file handle x is an exhausted iterator after the iteration. You would have to reset it to the beginning of the file after each counting loop. But it is way more efficient to count all letters in one iteration:

from collections import Counter cnt = Counter() with open(r"C:\Users\username\Downloads\abc.txt","r") as x: for i in x: cnt.update(i) for j in "abcdefghijklmnopqrstuvwxyz": print(cnt[j]) 
count = <> with open(r"C:\Users\username\Downloads\abc.txt","r") as x: srt = x.read() for i in srt: if i not in count: count[i] = srt.count(i) srt.replace(i,'') 

After iterating over the file the first time with the for i in x loop, the file is exhausted. All the other for i in x loops will not run at all.

Instead of iterating over the file directly, load the file contents into a variable which can be examined repeatedly.

Also, it looks like you don’t actually need to scan the file line-by-line; you can examine the entire file contents in one step.

with open(r"C:\Users\username\Downloads\abc.txt","r") as x: text = x.read() for letter in "abcdefghijklmnopqrstuvwxyz": frequency = text.count(letter) print(frequency) 

Iterate through a file lines in python, The simplest: with open (‘topology_list.txt’) as topo_file: for line in topo_file: print line, # The comma to suppress the extra new line char. Yes, you can iterate through the file handle, no need to call readlines ().

Iterator to iterate over Excel file

I have some data stored in Excel tables( .xlsx ), which my current Python script reads them into the memory and uses them for calculations. I will explain my script more with an example.

Say my excel file has this data under a specific column: a = [1,2,3,4,5] .

I am reading this whole thing into the memory using pandas ( pd.read_excel() ) and running my own iterator function to get:

a0 = [1,2,3,4,5] a1 = [5,1,2,3,4] a2 = [4,5,1,2,3] 

and so on. Basically I am shifting every element by some integer amount. a0, a1 and a2 here appear as lists but they are iterator objects, I don’t store them.

As you notice here, a0 is always the same as a , and I don’t really need to store a in memory because I only need it once, which is what a0 does. So what I am trying to do is having some sort of iterator object to iterate over the excel file directly to capture a0, a1 and a2 as if I were importing a first and then iterating for a0, a1, a2 over a .

The reason I am trying to do such a thing is because, the time my script takes for calculations are shorter than what it takes to import the data from Excel. So in order to increase my script in performance, I need to find a way to iterate over Excel rather than saving data into the memory. I would appreciate any help with this.

Addition, my comment: If pandas or some other library had readThisCell() kind of functionality, it would make things easy for me to make my own excel iterator. But I don’t know what my options are with pandas or any other library.

I don’t have experience with the pandas read_excel function, but we have had good success with openpyxl. That library lets you define a variable pointing to a specific worksheet and then iterate over that variable, as follows (pulled directly from their tutorial):

from openpyxl import load_workbook wb = load_workbook(filename='large_file.xlsx', read_only=True) ws = wb['big_data'] # ws is now an IterableWorksheet for row in ws.rows: for cell in row: print(cell.value) 

How to iterate over the file in python, This does NOT strip the trailing newlines: python -c ‘with open(«file.txt») as f: print(repr([l[-1] for l in f]))’ returns many instances of \n on Python 2.7.12 and 3.4.5 – JamesTheAwesomeDude May 2, 2017 at 4:02

Источник

Оцените статью