【发布时间】:2017-12-06 02:00:12
【问题描述】:
我正在尝试编写一个解析器来根据作者、标题和评论划分一个充满数据的 .txt 文件。我遇到了障碍,不知道下一步该做什么。我在 sez # 现在将评论与标题分开的单元格中只有一行代码(正则表达式代码)有问题
代码缺少撇号 (\')。当我尝试使用插入符号 ^ 阻止最后一段时,我得到一个空集。如果下面的源文本,我会包含一些内容,以便您可以看到我正在尝试解析的混乱。这很棘手!标题将直接流入期刊名称,例如 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
【问题讨论】:
-
你想达到什么目标,也许我们可以首先提供帮助?
-
您是否从网页中提取数据?也许原始页面结构将有助于整理它。
-
所以.txt文件是书评的索引。我需要清理数据,将其分成数据框中的三列:作者、标题、评论。