简体   繁体   English

如何遍历存储在变量 python 中的文件中的行

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

I have a decoded file from an html form stored in a variable:我有一个 html 形式的解码文件存储在一个变量中:

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

终端中的文件

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.我理想的 output 将是 7,因为那是表格从其列开始的时候。

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.使用enumerate function,然后搜索包含逗号的行,如果它确实用逗号分隔,然后继续您当前的方式。 You should also use a with statement when reading from files.从文件中读取时,您还应该使用with语句。 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 .如果您必须以自己的方式打开文件,那么它仍然是相同的,除了您还需要将整个文件拆分为\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)
# 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM