How to iterate through rows in a file which is stored in a variable python

Question:

I have a decoded file from an html form stored in a variable:

file_data = file.read().decode('utf-8')
print(file_data)

file in terminal

I want to iterate through the file with a counter that keeps track of the row number so that I can get the row number of when the table begins. These are the two ways I’ve tried to do it:

Method 1:

counter = 0
for row in file_data:
    if row == 'Date,Unique Id,Tran Type,Cheque Number,Payee,Memo,Amount':
        date_line_no = counter
        break
    counter += 1

Method 2:

for line, row in enumerate(file_data):
    first_column = row[0]
    if first_column == 'Date':
        print(row)
        date_line_no = line

My ideal output would be 7, as that is when the table begins with its columns.

Asked By: Nicholas Coetzee

||

Answers:

Use the enumerate function, then search for rows that contain a comma, If it does split it by commas and then continue the way your currently are. You should also use a with statement when reading from files. This avoids having to read the whole file in right away and ensures that it properly closes the file at the end.

for example:

with open(filename) as file:
    for i,row in enumerate(file):
        if ',' in row and row.split(',')[0] == 'Date':
            break
print(i)  # output is the index of the header row.

if you must open the file your way then its still the same, except you also need to split the whole file by n.

file_data = file.read().decode('utf-8')
print(file_data)


for i, row in enumerate(file_data.split('n')):
    if ',' in row and row.split(',')[0] == 'Date':
        break
print(i)
Answered By: Alexander
# Opening the file
file = open('your_file_name_or_location') 

# adding a counter for each line
for index, content in enumerate(file):

    var = content.split(',')   # Spliting each line by ','
    if var[0] == 'date':       
        break                  # Getting out of loop if first element is 'date'
print(index + 1)  # Position of header of the table
Answered By: Sanju Halder
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.