Search code examples
pythoniteratorxlsxattributeerrortext-extraction

Python: Iterate through text file; return a different line than referenced in 'if' statement


I'm attempting to iterate through a *.txt file and return lines (strings) that meet certain conditions--which will be written to an *.xlsx.

The code is working for the if "API" and "77-003" in line: condition, which returns the line in which "API" and "77-003" occur. However, the block shown below is still returning the line that includes "API", but not "67025". I want it to return the next line in the *.txt file. Here, I tried line = line.next() so that the next line would be returned.

elif "API" in line and "67025" not in line:
     line = line.next()
     api = line.rstrip('"\n').lstrip('API,"')
     worksheet.write(count, 0, api)
     worksheet.write(count, 1, count)

But I get the error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-20-a94ba137ca11> in <module>
     55             worksheet.write(count, 1, count)
     56         elif "API" in line and "42-003" not in line:
---> 57             line = line.next()
     58             api = line.rstrip('"\n').lstrip('API,"')
     59             worksheet.write(count, 0, api)

AttributeError: 'str' object has no attribute 'next'

I've tried multiple versions of this, but can't find an option that returns the next line successfully, instead of returning the line that has "API".

How would I return different line numbers than the one in the if statement? Any help is appreciated.

Example text file:

API 77-003-200
well dixie #1
API
67025650
well doodle #2
API 77-003-908
well dw washburn #1

What I want:

77-003-200   | 1
67025650     | 4
77-003-908   | 6

What I'm getting:

77-003-200   | 1
API          | 3
77-003-908   | 6

Abbreviated code below:

import xlsxwriter as xl
import os
from os import listdir

api = 0
row = 1

worksheet = xl.Workbook(myfile.xlsx').add_worksheet()
worksheet.write(0, 0, 'API')
worksheet.write(0, 1, 'Line Number')

count = 0

with open('C:/Project/file.txt', 'r', encoding="utf8") as text:
    for line in text:
        count += 1
        if "API" and "77-003" in line:
            api = line.rstrip('"\n').lstrip('API,"')
            worksheet.write(count, 0, api)
            worksheet.write(count, 1, count)
        elif "API" in line and "67025" not in line:
            line = line.next()
            api = line.rstrip('"\n').lstrip('API,"')
            worksheet.write(count + 1, 0, api)
            worksheet.write(count + 1, 1, count + 1)
        else:
            continue
workbook.close()

Solution

  • Here's an example of what I suggested in the comments, to use a flag to cause different processing of the next line of the file.

    import xlsxwriter as xl
    import os
    from os import listdir
    
    api = 0
    row = 1
    
    worksheet = xl.Workbook(myfile.xlsx').add_worksheet()
    worksheet.write(0, 0, 'API')
    worksheet.write(0, 1, 'Line Number')
    
    count = 0
    
    with open('C:/Project/file.txt', 'r', encoding="utf8") as text:
        flagged = False
        for line in text:
            count += 1
            if flagged:
                flagged = False
                api = line.rstrip('"\n').lstrip('API,"')
                worksheet.write(count + 1, 0, api)
                worksheet.write(count + 1, 1, count + 1)
            elif "API" in line and "77-003" in line:  # fixed an error here
                api = line.rstrip('"\n').lstrip('API,"')
                worksheet.write(count, 0, api)
                worksheet.write(count, 1, count)
            elif "API" in line and "67025" not in line:
                flagged = True
    workbook.close()