在这里看了很多解决方案,理解程度不一,想知道如果字符串是逗号分隔怎么办...
问题
在尝试处理联系信息的 csv 时,我需要解决这个问题:修剪多余的空格和一些垃圾,但保留尾随逗号和内部空格。使用包含联系人注释的字段,我想删除垃圾,留下好东西。修剪掉所有的标点符号和谷壳,我不想丢失复合标记之间的空格,因为我不想稍后重建。
正则表达式和模式:[\s_]+?\W+
该模式使用[\s_]+? 查找任何空白字符和下划线 ('_') 的单个实例,从 1 到无限次数(尽可能少的字符)出现在非单词字符之前1 到无限时间:\W+(相当于[^a-zA-Z0-9_])。具体来说,这会找到大量空白:空字符 (\0)、制表符 (\t)、换行符 (\n)、前馈 (\f)、回车 (\r)。
我认为这样做有两个好处:
它不会删除您可能想要放在一起的完整单词/标记之间的空格;
Python内置的字符串方法strip()不处理字符串内部,只处理左右两端,默认arg为空字符(见下例:文本中有几个换行符,@987654327 @ 不会在正则表达式模式下将它们全部删除)。 text.strip(' \n\t\r')
这超出了 OP 的问题,但我认为在很多情况下,我们可能会在文本数据中出现奇怪的病态实例,就像我所做的那样(转义字符如何在某些文本中结束)。此外,在类似列表的字符串中,我们不希望删除分隔符,除非分隔符分隔两个空白字符或一些非单词字符,如'-,' 或'-, ,,,'。
注意:不是在谈论 CSV 本身的分隔符。只有 CSV 中数据类似于列表的实例,即 c.s.子字符串的字符串。
完全披露:我只处理文本大约一个月,而正则表达式仅在过去两周内,所以我确信我缺少一些细微差别。也就是说,对于较小的字符串集合(我的是在 12,000 行和 40 奇数列的数据框中),作为通过删除无关字符的最后一步,这非常有效,特别是如果您在其中引入一些额外的空白想要分隔由非单词字符连接的文本,但不想在以前没有空格的地方添加空格。
一个例子:
import re
text = "\"portfolio, derp, hello-world, hello-, -world, founders, mentors, :, ?, %, ,>, , ffib, biff, 1, 12.18.02, 12, 2013, 9874890288, .., ..., ...., , ff, series a, exit, general mailing, fr, , , ,, co founder, pitch_at_palace, ba, _slkdjfl_bf, sdf_jlk, )_(, jim.somedude@blahblah.com, ,dd invites,subscribed,, master, , , , dd invites,subscribed, , , , \r, , \0, ff dd \n invites, subscribed, , , , , alumni spring 2012 deck: https: www.dropbox.com s, \n i69rpofhfsp9t7c practice 20ignition - 20june \t\n .2134.pdf 2109 \n\n\n\nklkjsdf\""
print(f"Here is the text as formatted:\n{text}\n")
print()
print("Trimming both the whitespaces and the non-word characters that follow them.")
print()
trim_ws_punctn = re.compile(r'[\s_]+?\W+')
clean_text = trim_ws_punctn.sub(' ', text)
print(clean_text)
print()
print("what about 'strip()'?")
print(f"Here is the text, formatted as is:\n{text}\n")
clean_text = text.strip(' \n\t\r') # strip out whitespace?
print()
print(f"Here is the text, formatted as is:\n{clean_text}\n")
print()
print("Are 'text' and 'clean_text' unchanged?")
print(clean_text == text)
这个输出:
Here is the text as formatted:
"portfolio, derp, hello-world, hello-, -world, founders, mentors, :, ?, %, ,>, , ffib, biff, 1, 12.18.02, 12, 2013, 9874890288, .., ..., ...., , ff, series a, exit, general mailing, fr, , , ,, co founder, pitch_at_palace, ba, _slkdjfl_bf, sdf_jlk, )_(, jim.somedude@blahblah.com, ,dd invites,subscribed,, master, , , , dd invites,subscribed, ,, , , ff dd
invites, subscribed, , , , , alumni spring 2012 deck: https: www.dropbox.com s,
i69rpofhfsp9t7c practice 20ignition - 20june
.2134.pdf 2109
klkjsdf"
using regex to trim both the whitespaces and the non-word characters that follow them.
"portfolio, derp, hello-world, hello-, world, founders, mentors, ffib, biff, 1, 12.18.02, 12, 2013, 9874890288, ff, series a, exit, general mailing, fr, co founder, pitch_at_palace, ba, _slkdjfl_bf, sdf_jlk, jim.somedude@blahblah.com, dd invites,subscribed,, master, dd invites,subscribed, ff dd invites, subscribed, alumni spring 2012 deck: https: www.dropbox.com s, i69rpofhfsp9t7c practice 20ignition 20june 2134.pdf 2109 klkjsdf"
Very nice.
What about 'strip()'?
Here is the text, formatted as is:
"portfolio, derp, hello-world, hello-, -world, founders, mentors, :, ?, %, ,>, , ffib, biff, 1, 12.18.02, 12, 2013, 9874890288, .., ..., ...., , ff, series a, exit, general mailing, fr, , , ,, co founder, pitch_at_palace, ba, _slkdjfl_bf, sdf_jlk, )_(, jim.somedude@blahblah.com, ,dd invites,subscribed,, master, , , , dd invites,subscribed, ,, , , ff dd
invites, subscribed, , , , , alumni spring 2012 deck: https: www.dropbox.com s,
i69rpofhfsp9t7c practice 20ignition - 20june
.2134.pdf 2109
klkjsdf"
Here is the text, after stipping with 'strip':
"portfolio, derp, hello-world, hello-, -world, founders, mentors, :, ?, %, ,>, , ffib, biff, 1, 12.18.02, 12, 2013, 9874890288, .., ..., ...., , ff, series a, exit, general mailing, fr, , , ,, co founder, pitch_at_palace, ba, _slkdjfl_bf, sdf_jlk, )_(, jim.somedude@blahblah.com, ,dd invites,subscribed,, master, , , , dd invites,subscribed, ,, , , ff dd
invites, subscribed, , , , , alumni spring 2012 deck: https: www.dropbox.com s,
i69rpofhfsp9t7c practice 20ignition - 20june
.2134.pdf 2109
klkjsdf"
Are 'text' and 'clean_text' unchanged? 'True'
因此 strip 一次删除一个空格。所以在 OP 的情况下,strip() 很好。但如果事情变得更复杂,正则表达式和类似的模式可能对更一般的设置有一些价值。
see it in action