【发布时间】:2013-06-12 10:51:48
【问题描述】:
我有一些代码在一个更大的程序中独立工作,但现在在更大的程序中它似乎不起作用——即它没有执行所需的操作。
问题出现在第 4 步(见下文),经过反思,我在字符类中的预期逻辑(即“除回车之外的所有内容”)似乎没有正确编码(但我没有知道如何“表达”逻辑)。
我的目标只是用段落标签包裹每一行或段落。
Python 代码
import re
# 1. open the html file in read mode
html_file = open('test.html', 'r')
# 2. convert to string
html_file_as_string = html_file.read()
# 3. close the html file
html_file.close()
# 4. replace carriage returns with closing and opening paragraph tags
html_file_as_string = re.sub('([^\r]*)\r', r'\1</p>\n<p>', html_file_as_string)
# 5. remove time and date
html_file_as_string = re.sub(r'(Lorem ipsum \d*/\d*/\d*, \d*:\d* [a-z]{2})', r"", html_file_as_string)
# 6. remove the white space after the opening paragraph tags
html_file_as_string = re.sub('<p>\n*\s*', r"<p>", html_file_as_string)
# 7. remove the white space before the closing paragraph tags
html_file_as_string = re.sub('\s*</p>', r"</p>", html_file_as_string)
# 8. open the file in write mode to clear
html_file = open('test.html', 'w')
# 9. write the new contents to file
html_file.write(html_file_as_string)
# 10. print to screen so we can see what is happening
print html_file_as_string
# 11. close the html file
html_file.close()
这是 HTML 文件的内容:
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Lorem ipsum..consectetur adipiscing elit.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.Lorem ipsum dolor sit amet, consectetur adipiscing elit."Lorem ipsum dolor sit amet", consectetur adipisc'ing elit.Lorem ipsum dolor...sit amet, consectetur adipiscing elit..
Lorem ipsum dolor sit amet, consectetur adipiscing elit.Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Lorem ipsum dolor sit amet, consectetur adipiscing elit..
.....Lorem ipsum dolor sit amet, consectetur adipiscing elit.Lorem ipsum dolor sit amet, consectetur adipiscing elit.Lorem ipsum dolor sit amet, consectetur adipiscing elit.Lorem ipsum dolor sit amet, consectetur adipiscing elit.Lorem ipsum dolor sit amet, consectetur adipiscing elit.Lorem ipsum 01/01/05, 05:00 am</p>
这是在 SciTE 编辑器中查看的文件内容(因此可以看到空格、回车和换行符)。
编辑:
我根据以下建议更改了正则表达式,然后将替换加倍(从第 4 步中可见的原始代码更改和第 4 步之前的第 6 步复制)。
工作代码:
import re
# 1. open the html file in read mode
html_file = open('test.html', 'r')
# 2. convert to string
html_file_as_string = html_file.read()
# 3. close the html file
html_file.close()
# 6(added). remove the white space after the opening paragraph tags
html_file_as_string = re.sub('<p>\n*\s*', r"<p>", html_file_as_string)
# 4(changed). replace carriage returns with closing and opening paragraph tags
html_file_as_string = re.sub('([^\r\n]*)(\r\n?|\n)', r'\1</p>\2<p>', html_file_as_string)
# 5. remove time and date
html_file_as_string = re.sub(r'(Lorem ipsum \d*/\d*/\d*, \d*:\d* [a-z]{2})', r"", html_file_as_string)
# 6. remove the white space after the opening paragraph tags
html_file_as_string = re.sub('<p>\n*\s*', r"<p>", html_file_as_string)
# 7. remove the white space before the closing paragraph tags
html_file_as_string = re.sub('\s*</p>', r"</p>", html_file_as_string)
# 8. open the file in write mode to clear
html_file = open('test.html', 'w')
# 9. write the new contents to file
html_file.write(html_file_as_string)
# 10. print to screen so we can see what is happening
print html_file_as_string
# 11. close the html file
html_file.close()
编辑 2:
上面的代码在其他部分代码过于激进,修改太多,回到画板。
【问题讨论】:
-
你有CRLF,这意味着
\r\n?然后你只需要让你的正则表达式更“通用”。试试([^\r\n]*)(\r\n?|\n),替换\1</p>\2<p> -
我只是描述一下我理解为这里模式的逻辑,为我自己,也可能为其他人。在第 1 组中,'除回车或换行之外的所有内容,重复 0 次或多次,在第 2 组中,'回车后跟换行 0 或 1 次或只是换行,这些组的替换是第 1 组,然后是
</p>,然后是第 2 组,然后是<p>。我尝试运行上面的代码,它看起来不错,除了第一个<p>[CR][LF]变成了<p></p>。 -
好的,我会建议(也许是更好的)方法来完成这项工作:1- 将您的文本拆分/分解
\r\n?|\n2- 从数组中删除所有空元素3- 附加到每个元素的开头<p>和末尾</p>。 -
我不太明白,但我认为根据这个逻辑做了一些更改并修改了上面的帖子,代码似乎在这个阶段工作。
-
呃,其他部分代码太激进,修改太多,回到绘图板。
标签: python regex python-2.7 newline carriage-return