如何使用python脚本以特定的间隔连接行?

2024-10-01 13:29:17 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用python脚本以特定的间隔连接行?你知道吗

我正在为比赛做文本分析。但这也有问题。在html扩展文件<p>1. *</p>中,问题以数字开头,后跟“.”行在下面给出的html扩展文件中。你知道吗

<p>1. Arrange the following words as per the order in dictionary.</p> <p>1. Inappropriate</p> <p>2. Inappeasable</p> <p>3. Inaptitude</p> <p>4. Inapplicable</p> <p>5. Inapprehensible</p> <p>(1) 25431</p> <p>(2) 13425</p> <p>(3) 24513</p> <p>(4) 52341</p> <p>Answer key: 3</p><p>This is the correct form as per the order in dictionary.</p><p>Inappeasable&gt; Inapplicable&gt; Inapprehensible&gt; Inappropriate&gt; Inaptitude</p> <p>1. Inappropriate</p> <p>2. Inappeasable</p> <p>3. Inaptitude</p> <p>4. Inapplicable</p> <p>2. Arrange the following words as per the order in dictionary.</p> <p>1. Venomous</p> <p>2. Ventrose</p> <p>3. Veneration</p> <p>4. Vengeance</p> <p>5. Ventilation</p> <p>(1) 43521</p> <p>(2) 31425</p> <p>(3) 43251</p> <p>(4) 34152</p> <p>Answer key: 4</p><p>This is the correct form as per the order in dictionary.</p><p>Veneration&gt; Vengeance&gt; Venomous&gt; Ventilation&gt; Ventrose</p>

输出文件必须如下….

如果有几行是问题的一部分,后面是数字,上面给出的“.”应该是问题本身的一部分,如下所示。。你知道吗

<p>1. Arrange the following words as per the order in dictionary.</p><p>1. Inappropriate</p><p>2. Inappeasable</p><p>3. Inaptitude</p><p>4. Inapplicable</p><p>5. Inapprehensible</p> <p>(1) 25431</p> <p>(2) 13425</p> <p>(3) 24513</p> <p>(4) 52341</p> <p>Answer key: 3</p><p>This is the correct form as per the order in dictionary.</p><p>Inappeasable&gt; Inapplicable&gt; Inapprehensible&gt; Inappropriate&gt; Inaptitude</p><p>1. Inappropriate</p><p>2. nappeasable</p><p>3. Inaptitude</p><p>4. Inapplicable</p> <p>2. Arrange the following words as per the order in dictionary.</p><p>1. Venomous</p><p>2. Ventrose</p><p>3. Veneration</p><p>4. Vengeance</p><p>5. Ventilation</p> <p>(1) 43521</p> <p>(2) 31425</p> <p>(3) 43251</p> <p>(4) 34152</p> <p>Answer key: 4</p><p>This is the correct form as per the order in dictionary.</p><p>Veneration&gt; Vengeance&gt; Venomous&gt; Ventilation&gt; Ventrose</p>

请帮帮我。。你知道吗


Tags: theingtdictionaryasorderfollowingwords
1条回答
网友
1楼 · 发布于 2024-10-01 13:29:17

最后,我能够为我提出的问题编写代码。代码如下所示。。你知道吗

import sys
import re
answerstart = re.compile('<p>\([12345]*\) ')
uestionstart = re.compile('^<p>[0123456789]*\. ')
data = open(sys.argv[1]).readlines()
allq = []
flag = False
for  line in reversed(data):
   if answerstart.match(line):
      if '<p>(1) ' in line:
        allq.append('\n'+line)
      else:
        allq.append(line)
   elif line.startswith('<p>Answer '):
      flag = False
      allq.append('\n'+line.rstrip())
   elif questionstart.match(line):
      if '<p>1. ' in line:
         allq.append(line.rstrip())
         flag =  True
      else:
         if flag == True:
            allq.append('\n'+line.rstrip())
         else:
            allq.append(line.rstrip())
            flag = False
   else:
      allq.append(line)
new = []
for line in reversed(allq):
   new.append(line)
print "".join(new)

但是在这段代码中有一个问题是,这将只适用于我在上面的问题中发布的格式。你知道吗

相关问题 更多 >