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
  Exploding multiple columns Mormolyce 2 2,685 Sep-07-2025, 02:13 PM
Last Post: Mormolyce
  Comparing two columns with same value but different font format doug2019 3 2,245 Jun-24-2025, 03:41 AM
Last Post: Pedroski55
  Get the string after a specific char JanJan 5 1,799 Apr-30-2025, 05:04 AM
Last Post: snl_9527
  Transform 3 Columns into Single Column DaveG 9 4,904 Mar-19-2025, 03:46 AM
Last Post: robbert23
  Building specific Python version on Raspberry PI 5 (Raspbian) andrewk 2 2,075 Feb-03-2025, 11:41 AM
Last Post: iterate
  Word matching with specific parameters CascadeDiver 3 2,082 Jan-28-2025, 02:10 PM
Last Post: perfringo
  Check time within specific time ranges basvdm 3 1,349 Jan-20-2025, 05:10 PM
Last Post: Gribouillis
  Help about a specific time selection QJZ 0 881 Dec-01-2024, 11:25 AM
Last Post: QJZ
  Install a module to a specific to Python Installation (one of many)) tester_V 2 5,377 Oct-29-2024, 03:25 PM
Last Post: snippsat
  Running search/replace across Polars dataframe columns efficiently hobbycoder 3 4,559 Oct-28-2024, 03:18 AM
Last Post: hobbycoder

Forum Jump:

User Panel Messages

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