PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 5, 2022

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

 September 05, 2022     dataset, python, sql, strip, trim     No comments   

Issue

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


Answered By - constantstranger
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing