- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有这个 CSV,上面写着下一行(请注意换行符/n):
"<a>https://google.com</a>",,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,Dirección
我正在尝试删除所有逗号并将地址放在第一行。因此,在 Python 上我使用这个:
with open('Reutput.csv') as e, open('Put.csv', 'w') as ee:
text = e.read()
text = str(text)
re.compile('<a/>*D', re.MULTILINE|re.DOTALL)
replace = re.sub('<a/>*D','<a/>",D',text) #arreglar comas entre campos
replace = str(replace)
ee.write(replace)
f.close()
据我所知,re.multiline 和 re.dotall 是满足/n 需求所必需的。我正在使用 re.compile 因为这是我知道的唯一添加它们的方法,但显然这里不需要编译它。
我该如何结束这段文字?
"<a>https://google.com</a>",Dirección
最佳答案
您根本不需要编译语句,因为您没有使用它。您可以将编译模式或原始模式放入 re.sub 函数中。您也不需要 MULTILINE 标志,它与您不使用的 ^ 和 $ 元字符的解释有关。
问题的核心是您正在将标志编译成正则表达式模式,但由于您没有在替代命令中使用编译后的模式,因此无法识别它。
还有一件事。 re.sub 返回一个字符串,所以 replace = str(replace)
是不必要的。
这是对我有用的:
import re
with open('Reutput.csv') as e:
text = e.read()
text = str(text)
s = re.compile('</a>".*D',re.DOTALL)
replace = re.sub(s, '</a>"D',text) #arreglar comas entre campos
print(replace)
如果不编译直接调用re.sub,需要这样调用
re.sub('</a>".*D', '</a>"D', text, flags=re.DOTALL)
当然,我不知道你的具体申请是什么,但是如果你只想删除所有的逗号和换行符,这样写可能会更清楚
replace = ''.join((c for c in text if c not in ',\n'))
关于python re.sub换行多行dotall,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32018434/
我在产品类别和子类别网站上工作,可以创建的子类别数量是无限的,这意味着我们可以自己拥有子类别的子类别。一切正常,我只是有一个问题:假设“PRODUCT 1”属于“SUB SUB CATEGORY 1”
我正在学习OCaml,并在代码2022的到来中努力工作。。但似乎读取input.txt文件会导致一些问题。。我收到了这个奇怪的错误,它来自于函数分数。尤其是那个叫得分手HD的表达。不过,当我手动将LI
我有一个奇怪的行为(对于 Python 程序员)子例程,它简化如下: use strict; use Data::Dumper; sub a { my @x; sub b { push @x,
在此页上 http://bit.ly/T3yJIH , drupal 的 Nice Menu 模块不能正确显示子子菜单。如果转到菜单中的第一项,然后转到子菜单中的第一项,就可以看到它。 (Facili
我需要在我的站点内创建一个子结构,我在其中拥有: 实际上我是通过 Javascript 来完成的(伪代码):SUB_Content.height = ParentDiv.height - Sub_He
我有一个与子菜单一起使用的 CSS 手册。我想知道如何向它添加子子菜单。例如,我将鼠标悬停在主菜单项上并弹出子菜单,然后我将鼠标悬停在子菜单项上并弹出另一个子菜单。这是我现在使用的 JS Fiddle
我有一个关于 navmenu 的问题我不能选择导航菜单的 sub sub ul,如果我会做什么它会影响 3.ul。这是一个 wordpress 菜单我没有机会使用类或菜单的 ID。提前感谢您的帮助。
我创建了一个带有 2 级子菜单的简单导航。我正在寻找“sub sub”选项以显示在其父项的右侧而不是下方。对于 CSS 的任何帮助,我将不胜感激。下面是 html 和 css 以及 JS fiddle
我对 VBA、这个论坛和编程很陌生。我有一个工作表,我已经设法根据我的要求谷歌并调整某些代码行。 我的问题是我总共有三个潜艇,必须逐步运行每个 VBA 脚本。我希望将所有三个 VBA 脚本合二为一。
我使用 sub 调用作为另一个 sub 的参数。示例代码: test(isInString(), 'second parameter', 'third parameter'); sub test {
我想使用 HTML ui->FresBox->setText("fres"); 但它在 QCheckbox 中不起作用。如果您使用标签,它会很好用。有什么不同以及如何在 QCheckbox 中使用 H
我没有遇到这个问题,但在处理相关问题时想到了这个问题。让我们: logging = 'something' # bad naming decision import logging as lg
我正在查看 Redis pubsub,消费者是 2-4 节点自动缩放组的一部分(HA 原因) 也许我遗漏了一些明显的东西,但是 Redis pubsub 中是否有某种机制可以管理订阅者/消费者收到的重
对你们来说应该是一个快速和简单的,为什么这不起作用? 代码限制用户只能在文本框中输入文本。 它工作正常,但我有大约 50 个文本框,所以会更干净,更容易调用。 但是,这样做,限制不再起作用 Priva
(编辑)TL; DR :我的问题是,尽管Win32 API定义的是真实的整数常量(如平台SDK header 中一样),而Win32 Perl包装器将它们定义为subs。从而引起一线解析的误解。 在单
我有一个使用 Jersey 实现 REST API 的 Web 应用程序。 Web 容器 id Tomcat 以下是 API 的摘要: /rest/patients 获取患者元数据列表。 /rest/
我与 Czech 一起工作Python 3.4 中的重音文本。 调用 re.sub()用正则表达式对重音句子进行替换效果很好,但使用用 re.compile() 编译的正则表达式然后调用regex.s
对于我的一个新项目,我必须创建一个包含主类别、子类别和子子类别的类别页面。例如; 互联网 互联网 > 发展 互联网 > 开发 > 移动应用 在此类别中,您可以找到有关这些内容的网站。我必须为每个网站提
表1 - 客户数据 c_id(整数) 名称(varchar) 表2 - account_data a_id(整数) c_id (int) -> 使用customer_data。c_id plan_id
我有一些位图文件(jpeg、png、...),我想编写一个 C++ 程序,将这些位图文件连接到 pes 流(mpeg-2 格式),然后创建一个子文件(.sub/. idx:vobsub 字幕文件)。
我是一名优秀的程序员,十分优秀!