Search code examples
pythonsqldatasettrimstrip

How do I remove certain parts from a string in a dataset in python?


I have a data set that has a code and then a name with a code, and there multiple codes and multiple names example:

|CODE|NAME|

|TN |Tobey JacobsTN|

|GD |Lars OwensGD|

|YO |Mark SmithYO|

|BM |John SawyerBM|

etc...

How would I take the code out and just leave the name using python?


Solution

  • You could do something like this:

        strings = [
            '|CODE|NAME|',
            '|TN |Tobey JacobsTN|',
            '|GD |Lars OwensGD|',
            '|YO |Mark SmithYO|',
            '|BM |John SawyerBM|'
        ]
        records = [[field.rstrip(' ') for field in s.split('|')[1:-1]] for s in strings][1:]
        print("records:"); [print(x) for x in records]
        names = [name[:-len(code)] for code, name in records]
        print("names:"); [print(x) for x in names]
    

    ... which gives the following output:

    records:
    ['TN', 'Tobey JacobsTN']
    ['GD', 'Lars OwensGD']
    ['YO', 'Mark SmithYO']
    ['BM', 'John SawyerBM']
    names:
    Tobey Jacobs
    Lars Owens
    Mark Smith
    John Sawyer