Python Forum
Remove Specific Columns when the number of columns is greater than a specific value - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Remove Specific Columns when the number of columns is greater than a specific value (/thread-34872.html)



Remove Specific Columns when the number of columns is greater than a specific value - CuriousOne - Sep-09-2021

I need to remove 2 columns (column 41 and 42) from a row when the column count within a segment of the row is > 42. I'm extremely new to Python. How would I add it to this section? I want to add if category = 'ABC' and countFields(line) > 42 the remove columns 41 and 42.
ret = []
    for line in file:
        category = line[0:3]
        line = line[4:].strip()
        if category in ('FHS', 'BHS', 'MSH', 'FTS'):
            continue
        elif category == 'PID':
            if _countFields(line) == 18:
                line += "|"
            curPatient = _Patient(line)
            ret.append(curPatient)
        else:
            curPatient.addData(line, category)
    file.close()
    return ret