如何从 python 中的数据集中的字符串中删除某些部分?
How do I remove certain parts from a string in a dataset in python?
我有一个数据集,它有一个代码,然后是一个带代码的名称,还有多个代码和多个名称示例:
|代号|名称|
|TN |Tobey JacobsTN|
|GD |Lars OwensGD|
|YO |马克史密斯YO|
|BM |约翰·索亚BM|
等...
我如何取出代码并使用 python 留下名称?
你可以这样做:
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]
... 给出以下输出:
records:
['TN', 'Tobey JacobsTN']
['GD', 'Lars OwensGD']
['YO', 'Mark SmithYO']
['BM', 'John SawyerBM']
names:
Tobey Jacobs
Lars Owens
Mark Smith
John Sawyer
我有一个数据集,它有一个代码,然后是一个带代码的名称,还有多个代码和多个名称示例:
|代号|名称|
|TN |Tobey JacobsTN|
|GD |Lars OwensGD|
|YO |马克史密斯YO|
|BM |约翰·索亚BM|
等...
我如何取出代码并使用 python 留下名称?
你可以这样做:
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]
... 给出以下输出:
records:
['TN', 'Tobey JacobsTN']
['GD', 'Lars OwensGD']
['YO', 'Mark SmithYO']
['BM', 'John SawyerBM']
names:
Tobey Jacobs
Lars Owens
Mark Smith
John Sawyer