- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一些代码独立于一个更大的程序工作,但现在在更大的程序中它似乎不起作用——即它没有执行所需的操作。
问题出现在第 4 步(见下文),经过反射(reflection),我在字符类中的预期逻辑(即“除回车外的所有内容”)似乎没有正确编码(但我没有知道如何“表达”逻辑)。
我的目标只是用段落标签包裹每一行或每一段。
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:
上面的代码在代码的其他部分过于激进,做了太多的修改,回到绘图板。
最佳答案
我认为您最好遍历字符串,而不是像 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)
关于python - 在 Python 中使用正则表达式定位回车符和换行符的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17131353/
这里是新的,通常在 VBA 中。我创建了一个宏,它可以复制 excel 中单元格的内容并粘贴到 word 文档中的特定位置。仅供引用,我使用 word 中的书签来选择粘贴的确切位置。问题是复制的所有内
我正在阅读有关字符串文字的 MySql 文档,并尝试使用回车符进行以下操作: 选择“我想要一个\r bannana”; 为什么会返回: +--------------------+ bannana
我正在运行以下代码: #include #include #include int main(){ FILE *fp; if((fp=fopen("test.txt","r"))==N
我一直想知道为什么在运行每个应用程序时命令提示符和Powershell都预先装载了这么多回车符。您可以永远滚动(好吧,不是真的,而是几十个,如果不是数百个页面长度的空白空间)。我现在没有Unix机器,
回车、换行、换行有什么区别?它取决于操作系统吗?为什么我们需要使用所有这些来到达下一行? 最佳答案 通常,“新行”是指通常被解释为表示新行的任何字符集,其中可以包括: CR LF(在 DOS/Wind
#include #include #include struct st{ char n[100]; //Name char
我对 php/mysql 比较陌生。我正在尝试对这部分进行回车: echo "" . nl2br($row['dish_description']) . ""; “dish_description”包
我正在阅读一个普通的文本文件,并将所有单词作为数字写入另一个文本。当一行结束时,它会查找“换行符 (\n)”并从新行继续。在 Ubuntu 中它可以完美执行,但在 Windows (DevC++) 中
Python Docx 是一个非常好的库,用于为不直接处理所有 COM 内容的内容生成 Microsoft Word 文档。尽管如此,我还是遇到了一些限制。 有谁知道如何在一串文本中加入回车符? 我希
我正在使用 CLion IDE,我正在尝试执行回车。 我正在用 C 语言执行打印语句,语法如下: printf("\rHello World!"); 这是一个循环。循环仍然在自己的行上打印每个 Hel
我正在尝试使用正则表达式来匹配字符串和 \r 之间的每个字符特点 : text = 'Some text\rText to find !\r other text\r' 我要配'Text to fin
使用 knockout.js,如何在绑定(bind)到段落文本属性的文本中包含回车 元素。 在我的 ViewModel 中,我生成了一个绑定(bind)到 的文本字符串。在 View 中。我想在浏览
# make test cc test.c -o test # ./test I am a happy yam until i hit return... # ./test because
我正在使用 Moodle 2.2.1,但有一个基本的 php-mysql 问题。我试图返回 WHERE 子句中带有条件的字段的计数。 WHERE 子句条件来自 foreach 循环。这是我的代码:
我在@Html.TextBoxFor 中显示一些长文本。文本包含换行符、回车符等,但@Html.TextBoxFor 似乎不尊重这些。有什么我可能遗漏的想法吗? 最佳答案 文本框每个定义单行。 您必须
隔离问题一小时后,我可以发布这个:) 我的本地文件有问题,但在生产环境中没有。唯一的区别是在生产环境中,html 文件是内联的。 我正在使用默认的 bootstrap 3.3.5 和这个 css
在过滤选项后面加上: \t[|] \ r[|] 也就是C语言中的回车和空格 附上C语的其他转义字符以便急之需: \a 响铃(BEL) 007 \b 退格(BS) 008 \f 换页(F
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界. 这篇CFSDN的博客文章java去除字符串中的空格、回车、换行符、制表符的小例子由作者收集整理,
我正在尝试编写一份将在 Excel 中发送的报告,该报告将两条记录连接到同一单元格中,并在每条记录之间显示回车符。我一直在尝试 ||chr(10) || chr(13) ||但这仅在我作为脚本运行时有
我的 fopen 是这样设置的。我尝试过使用“t”和不使用“t”的 fopen。由于某种原因,我的 fprintf 打印出 ^M ,它们是回车符。如何阻止 frpintf 这样做?我想只使用普通的换行
我是一名优秀的程序员,十分优秀!