這是我之前的一個問題的后續,我更清楚地發現了這個問題,我需要一些進一步的建議:)
我有一個字串,由一些機器學習演算法產生,它通常具有以下結構:
- 在開頭和結尾,可以有一些行不包含任何字符(空格除外);
- 中間應該有 2 行,每行包含一個名字(或者只有姓氏,或者名字和姓氏,或者名字的首字母加上姓氏......),然后是一些數字和(有時)混合的其他字符在數字之間;
- 其中一個名稱通常以特殊的非字母數字字符(>、>>、@、...)開頭。
像這樣的東西:
Connery 3 5 7 @ 4
>> R. Moore 4 5 67| 5 [
我需要提取 2 個名稱和數字字符,并檢查其中一行是否以特殊字符開頭,因此我的輸出應該是:。
name_01 = 'Connery'
digits_01 = [3, 5, 7, 4]
name_02 = 'R. Moore'
digits_02 = [4, 5, 67, 5]
selected_line = 2 (anything indicating that it's the second line)
在鏈接的原始問題中,有人建議我使用:
inp = '''Connery 3 5 7 @ 4
>> R. Moore 4 5 67| 5 ['''
lines = inp.split('\n')
for line in lines:
matches = re.findall(r'\w ', line)
print(matches)
產生的結果非常接近我想要的結果:
['Connery', '3', '5', '7', '4']
['R', 'Moore', '4', '5', '67', '5']
但是我需要將第二行中的前兩個字串('R'、'Moore')組合在一起(基本上,在數字開始之前將所有字符組合在一起)。并且,它會跳過特殊字符的檢測。我應該以某種方式修復這個輸出,還是可以用完全不同的方式解決這個問題?
uj5u.com熱心網友回復:
我不確定您希望保留或洗掉哪些字符,但以下內容應該適用于該示例:
inp = '''Connery 3 5 7 @ 4
>> R. Moore 4 5 67| 5 ['''
lines = inp.split('\n')
for line in lines:
matches = re.findall(r'(?:[a-zA-Z.][a-zA-Z.\s] [a-zA-Z.])|\w ', line)
print(matches)
輸出:
['Connery', '3', '5', '7', '4']
['R. Moore', '4', '5', '67', '5']
注意。我包括a-z(下和上)和點,中間有可選的空格: [a-zA-Z.][a-zA-Z.\s] [a-zA-Z.],但您應該根據您的實際需要進行更新。
uj5u.com熱心網友回復:
這還將包括特殊字符(請記住,它們是硬編碼的,因此您必須將缺少的字符添加到正則運算式部分[>@] )
for line in lines:
matches = re.findall(r'(?:[a-zA-Z.][a-zA-Z.\s] [a-zA-Z.])|\w |[>@] ', line)
print(matches)
uj5u.com熱心網友回復:
這最好分幾個步驟完成。
# get the whitespace at start and end out
lines = inp.strip().split('\n')
for line in lines:
# for each line, identify the selection mark, the name, and the mess at the end
# assuming names can't have numbers in them
match = re.match(r'^(\W )?([^\d] ?)\s*([^a-zA-Z] )$', line.strip())
if match:
selected_raw, name, numbers_raw = match.groups()
# now parse the unprocessed bits
selected = selected_raw is not None
numbers = re.findall(r'\d ', numbers_raw)
print(selected, name, numbers)
# output
False Connery ['3', '5', '7', '4']
True R. Moore ['4', '5', '67', '5']
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/337497.html
下一篇:如何快速將大量資料轉換為字串?
