Python中使用正则表达式定位回车和换行时出现的问题

Python中使用正则表达式定位回车和换行时出现的问题,python,regex,python-2.7,newline,carriage-return,Python,Regex,Python 2.7,Newline,Carriage Return,我有一些代码是在一个更大的程序中独立运行的,但是现在在这个更大的程序中它似乎不工作了——也就是说,它没有执行所需的操作 问题发生在第4步(见下文),经过反思,我在character类中的预期逻辑(即“除回车外的所有内容”)似乎没有正确编码(但我不知道如何“短语化”逻辑) 我的目标只是用段落标记来包装每一行或每一段 Python代码 import re # 1. open the html file in read mode html_file = open('test.html', 'r')

我有一些代码是在一个更大的程序中独立运行的,但是现在在这个更大的程序中它似乎不工作了——也就是说,它没有执行所需的操作

问题发生在第4步(见下文),经过反思,我在character类中的预期逻辑(即“除回车外的所有内容”)似乎没有正确编码(但我不知道如何“短语化”逻辑)

我的目标只是用段落标记来包装每一行或每一段

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()
重新导入
# 1.  以读取模式打开html文件
html\u file=open('test.html','r')
# 2.  转换为字符串
html\u file\u as\u string=html\u file.read()
# 3.  关闭html文件
html_file.close()
# 4.  用结束和开始段落标记替换回车
html_file_as_string=re.sub('([^\r]*)\r',r'\1

\n',html_file_as_string) # 5. 删除时间和日期 html_file_as_string=re.sub(r'(Lorem ipsum\d*/\d*/\d*,\d*:\d*[a-z]{2}),r'',html_file_as_string) # 6. 删除开头段落标记后的空白 html\u file\u as\u string=re.sub(“\n*\s*”,r“”,html\u file\u as\u string) # 7. 删除结束段落标记前的空白 html\u file\u as\u string=re.sub('\s*

',r“

”,html\u file\u as\u string) # 8. 以写模式打开文件以清除 html\u file=open('test.html','w') # 9. 将新内容写入文件 html\u file.write(html\u文件作为字符串) # 10. 打印到屏幕上,这样我们就可以看到发生了什么 将html文件打印为字符串 # 11. 关闭html文件 html_file.close()
以下是HTML文件的内容:


Lorem ipsum dolor sit amet,是一位杰出的献身者。
洛雷姆·伊普苏姆……献祭精英。
Lorem ipsum dolor sit amet,是一位杰出的献身者。Lorem ipsum dolor sit amet,是一位杰出的献身者。
我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我爱我。。
我爱你,我爱你。我爱你,我爱你,我爱你。
Lorem ipsum dolor sit amet,奉献精英。。
这句话的意思是:“我的天啊!我的天啊!我的天啊!我的天啊!我的天啊!我的天啊!我的天啊!我的天啊!我的天啊!我的天啊!我的天啊!我的天啊!我的天啊!我的天啊!我的天啊!我的天啊!我的天啊!”

以下是在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()
重新导入
# 1.  以读取模式打开html文件
html\u file=open('test.html','r')
# 2.  转换为字符串
html\u file\u as\u string=html\u file.read()
# 3.  关闭html文件
html_file.close()
#6(增加)。删除开头段落标记后的空白
html\u file\u as\u string=re.sub(“\n*\s*”,r“”,html\u file\u as\u string)
#4(已更改)。用结束和开始段落标记替换回车
html_file_as_string=re.sub(“([^\r\n]*)(\r\n?|\n)”,r“\1

\2”,html_file_as_string) # 5. 删除时间和日期 html_file_as_string=re.sub(r'(Lorem ipsum\d*/\d*/\d*,\d*:\d*[a-z]{2}),r'',html_file_as_string) # 6. 删除开头段落标记后的空白 html\u file\u as\u string=re.sub(“\n*\s*”,r“”,html\u file\u as\u string) # 7. 删除结束段落标记前的空白 html\u file\u as\u string=re.sub('\s*

',r“

”,html\u file\u as\u string) # 8. 以写模式打开文件以清除 html\u file=open('test.html','w') # 9. 将新内容写入文件 html\u file.write(html\u文件作为字符串) # 10. 打印到屏幕上,这样我们就可以看到发生了什么 将html文件打印为字符串 # 11. 关闭html文件 html_file.close()
编辑2:


上面的代码在代码的其他部分过于激进,做了太多的修改,回到绘图板上。

我认为您最好遍历字符串,而不是像Python一样尝试使用正则表达式。您只需几步就可以完成这项工作

import re
parsed_html = []

# Open the file and close it after being read
with open('test.html', 'r') as html_file:
    lines = html_file.readlines()

# Iterate through each line using \r\n, \r or \n as separators 
for line in lines:
    # Remove whitespace chars before and after the content (6 & 7)
    line = line.strip()

    # Skip empty lines (you could merge this and latter if)
    if not line:
         continue

    # Skip lines with only <p> or </p> (check output and remove if not needed)
    if re.match("^</?p>$", line):
         continue

    # Remove the date line (using + instead of * as * matches 0 or more entries)
    line = re.sub(r'Lorem ipsum \d+/\d+/\d+, \d+:\d+ (am|pm)', '', line)

    # Make sure we add p tag and CL/LR to the end of each line
    line = "<p>{0}</p>\r\n".format(line)

    # Append current line to a list that we will use to write the file
    parsed_html.append(line)

# Write contents of the parsed html to the file
with open('test.html', 'w') as html_file:
    html_file.writelines(parsed_html)
重新导入
已解析的_html=[]
#打开文件并在读取后将其关闭
打开('test.html','r')作为html\u文件:
lines=html\u file.readlines()
#使用分隔符\r\n、\r\n或\r\n遍历每一行
对于行中的行:
#删除内容前后的空白字符(6和7)
line=line.strip()
#跳过空行(如果需要,可以合并此行和后一行)
如果不是直线:
持续
#仅使用或跳过行(检查输出,如果不需要则删除)
如果重新匹配(“^$”,第行):
持续
#删除日期行(使用+而不是*作为*匹配0个或多个条目)
line=re.sub(r'Lorem ipsum\d+/\d+/\d+,\d+:\d+(am | pm)',,第行)
#确保我们在每行末尾添加p标签和CL/LR
line=“{0}

\r\n”。格式(行) #将当前行附加到我们将用于写入文件的列表 解析的html.append(行) #将解析后的html内容写入文件 打开('test.html','w')作为html\u文件: html\u file.writelines(已解析的html)
您有CRLF,意思是
\r\n
?然后你只需要让你的正则表达式更“通用”。尝试
([^\r\n]*)(\r\n?|\n)
,替换
\1

\2
,我只想为我自己和其他人描述一下我所理解的模式逻辑。在第1组中,“除回车或换行外的所有内容,重复0次或多次;在第2组中,“回车后加换行0次或1次或JU”