0

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.

0

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

3 Comments

for some reason the rest of my code after running this doesn't work, and it doesn't even print the value of i. Do you have any idea what could be causing this?
@NicholasCoetzee Not without you posting more of your code. If you do decide to post more please copy and paste it instead of posting images
@NicholasCoetzee try the bottom example again...there was a bug
0
# 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

1 Comment

Your answer could be improved by adding more information on what the code does and how it helps the OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.