我正在尝试编写一个解析器,根据作者,标题和评论来分割一个充满数据的.txt文件。我遇到了障碍,不知道接下来该做什么。我只有一行代码,正则表达式代码,在单元格中有问题#send现在将评论与标题分开
代码错过了撇号(\')。当我尝试使用插入符^来阻止最后一段时,我得到一个空集。如果下面的源文本我包括一点,所以你可以看到我正在尝试解析的混乱。这很棘手!标题将直接流入日记的名称,如Choice。所以我试图通过切断紧跟在\ s- \ s模式之前的单词来分离。
以下是代码:
with open(file) as f:
content = f.readlines()
content = [x.strip() for x in content]
content = " ".join(content)
# Get all authors
pattern = r"[A-Z\-]{2,}[\,]+\s[A-Za-z\s\,\(\)\.]+\s[\-\*\•\.\■ ]{1}"
authors = re.findall(pattern, content)
# Now replace all found authors with XXX_XXX
if re.search(pattern, content):
r = re.compile(pattern)
content2 = r.sub(r'XXX_XXX', content)
# Now get all the content for each author
content3 = content2.split('XXX_XXX')
bib = content3[1:]
# Now separate reviews from titles
**pattern2 = r"[A-Z][a-z][\w\'\-\:\;\s\(\)]+\w+\s\-\s"**
bib2 = "".join(bib)
titles = re.findall(pattern2, bib2)
这条线,pattern2,我无法开始工作。源代码如下:
MA, Huan • The Overall Survey Of The Ocean’s Shores 1433
Choice - v8 - 0 ’71 - pl074 MA, Huan • Ying-Yai Sheng-Lan AHR - v76 - D ’71 - pl578 GJ - vl37 - Je ’71 - p213 JAS - v31 - N ’71 - pl81 TLS - Je 16 ’72 - p681 MA, Laurence J C - Commercial Development And Urban Change In Sung China 960-1279
JAS - v31 - Ag ’72 - p928 Pac A - v45 - Summer ’72 - p285 MA, Laurence J C - The Environment JAS - v42 - N ’82 - pl39 MA, Laurence J C - Urban Development In Modern China
Choice - vl9 - Ja ’82 - p696 JAS - v42 - N 82 - pl39 MA, Nancy Chih - Cook Chinese AB - v45 - My 25 ’70 - pl786 PW - vl97 - Mr 23 ’70 - p38 MA, Nancy Chih • Don’t Lick The Chopsticks CSM - v66 - Ja 10 ’74 - pF2 LJ - v99 - Mr 15 ’74 - p757 MA, Nancy Chih - Mrs. Ma’s Japanese Cooking
VQR - v58 - Spring ’82 - p68 MA, Tsu Sheng - Microscale Manipulations In Chemistry
Choice-vl3-N ’76 -pi 164 MA, Tsu Sheng - Organic Functional Group Analysis By Gas Chromatography Choice - vl3 - F ’77 - pl624 r MA, Wei-Yi - A Bibliography Of Chinese-Language Materials On The People's Communes ARBA - vl5 - '84 - p320
Pac A - v56 - Winter ’83 - p796 MA, Wook - Seoul Ro Kanun Kil BL - v78 - 0 15 '81 - p294 y MA, Y W - Traditional Chinese Stories ANQ - vl8 - 0 ’79 - p30 BF - v4 - Ap 40 '79 - p575 Choice -vl5-Ja ’79 -pl528 HR-v32-Spring'79-pl23 JAS - v38 - Ag '79 - p773 Kliatt - vl3 • Winter '79 - p26 WIT - v53 - Summer '79 - p555 MA, Yun • Shih Ching T'ao Hsing BL - v68 - Ap 1 '72 - p651 MA BRICALL, Josep - Politica Economica De La Generalitat 1936-1939. Vol. 1 WP - v25 - O '72 - pl55 MA COY, Ramelle • Short-Time Compensation
Choice - v21 - Jl '84 - pl648 Econ Bks - vll - S ’84 - p62 c MA De - The Cowherd And The Weaving Maid
Cur R - v20 - S '81 -p325 c MA De - Crickets
Cur R - v20 - S '81 - p325 c MA De - School-Master Dongguo Cur R - v20 - S '81 - p325 c MA De - Thrice Borrowing The Plantain Fan CurR- v20-S ’81 -p325 c MA De - The Wonderful Gourds Cur R - v20 - S '81 - p325 MAACK, Berthold - Preussen JMH - v55 - Mr '83 - p71 r MAACK, Mary N - Libraries In Senegal ARBA - vl3 - '82 - pi53 CRL - v45 - Mr '84-pl52 JAL - v7 - S '81 - p244 JLH - vl9 - Spring ’84 - p315 LJ - vl07 - My 1 ’82 - p865 LQ - v52 - Ap '82-pl75 MAACK, Reinhard • Kontinentaldrift Und Geologie Des Sudatlantischen Ozeans GJ - vl36 - Mr '70 - pl38 MAAG, Russell C - Observe And Understand The Sun
S&T - v54 - S ’77 - p221 MAAG, Victor - Hiob
Rel St Rev - vlO - Ap '84 - pi 75 MAAILMA Katettu Poyta
WIT - vS8 • Winter '84 - pi 36 MAALOE, Ole - Control Of Macromolecular Synthesis
Choice - v3 - 0 '66 - p676 Sci - vl54 - D 2 '66 - pll59 MAALOUF, Amin • The Crusades Through Arab Eyes
TLS -N 16 ’84 -pi300 c MAAR, Len - Out-Of-Sight Games CBRS - v9 - F ’81 - p57 SLJ-v27 - Mr ’81 -pl48 p MAARAV
Choice - vl6 - D '79 - pl280 MAAREK, Gerard • Introduction Au Capital De Karl Marx
JEL - vl7 - Mr ’79 - p92 MAAS, Audrey Gellen • Wait Till The Sun Shines, Nellie
答案 0 :(得分:0)
您可以直接匹配记录及其部分而不是执行 分裂,子和其他东西。
最后,你只是在做同样的事情。
我知道这很难。它就像一只骨头如此大的狗,它不知道如何 接近它。
我试图将部件分解得更好一些。
第1组包含作者 第2组包含标题 第3组包含misc内容(剩下的内容直到下一位作者)。
我所做的就是让每个样本记录都正确匹配 如果样本足够多,这通常涵盖所有基础。
如果您发现特殊情况,则必须将其加入代码中。
祝你好运!需要注意的事项 - Misc 部分使用否定断言来测试每个 角色直到下一个作者 如果您对作者部分进行了更改,则必须将该更改复制为
这个断言。
(?s)([A-Z-]{2,},*\s(?:[A-Za-z\s,().]|[A-Za-z]-[A-Za-z])+)\s[-*•.■ ]\s*(?:([A-Z](?:(?!\s+-|\s+Choice\s*-).)*?\w)(?:\s+-\s*|\s+(?=Choice\s*-)|\s*$))?((?:(?![A-Z-]{2,},*\s(?:[A-Za-z\s,().]|[A-Za-z]-[A-Za-z])+\s[-*•.■ ]).)*)
https://regex101.com/r/dg7hAB/1
(?s) # Dot-all modifier
( # (1 start), Required Author
[A-Z-]{2,}
,* # Optional comma's
\s
(?: # Author characters, add more if needed
[A-Za-z\s,().]
| [A-Za-z] - [A-Za-z] # allow hyphen between letters only
)+
) # (1 end)
\s
[-*•.■ ] # Author ender
\s*
(?:
( # (2 start), Optional Title
[A-Z]
(?: # Anything except title enders
(?!
\s+ -
| \s+ Choice \s* -
)
.
)*?
\w # Should end with a word char
) # (2 end)
(?: # Title enders
\s+ - \s*
| \s+
(?= Choice \s* - ) # Choice to be picked up by Misc Info
| \s* $
)
)?
( # (3 start), Optional Misc Info
(?: # Anything except start of new author
(?!
[A-Z-]{2,}
,*
\s
(?:
[A-Za-z\s,().]
| [A-Za-z] - [A-Za-z]
)+
\s
[-*•.■ ]
)
.
)*
) # (3 end)