Python Forum
Remove Specific Columns when the number of columns is greater than a specific value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove Specific Columns when the number of columns is greater than a specific value
#1
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
Larz60+ write Sep-09-2021, 09:23 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get the string after a specific char JanJan 5 396 Apr-30-2025, 05:04 AM
Last Post: snl_9527
  Transform 3 Columns into Single Column DaveG 9 3,607 Mar-19-2025, 03:46 AM
Last Post: robbert23
  Building specific Python version on Raspberry PI 5 (Raspbian) andrewk 2 755 Feb-03-2025, 11:41 AM
Last Post: iterate
  Word matching with specific parameters CascadeDiver 3 970 Jan-28-2025, 02:10 PM
Last Post: perfringo
  Check time within specific time ranges basvdm 3 665 Jan-20-2025, 05:10 PM
Last Post: Gribouillis
  Help about a specific time selection QJZ 0 435 Dec-01-2024, 11:25 AM
Last Post: QJZ
  Install a module to a specific to Python Installation (one of many)) tester_V 2 2,235 Oct-29-2024, 03:25 PM
Last Post: snippsat
  Running search/replace across Polars dataframe columns efficiently hobbycoder 3 2,403 Oct-28-2024, 03:18 AM
Last Post: hobbycoder
  recurring events every specific amount of days jacksfrustration 6 2,063 Jun-27-2024, 01:13 PM
Last Post: deanhystad
  How to compare specific elements of a TSV in difflib gonksoup 1 959 Jun-08-2024, 02:05 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020