简体   繁体   English

使用split()时如何使标点符号成为单独的项目

[英]how to make punctuation a separate item when using split()

I'm writing a program that compresses text by replicating it with a sequence of numbers - but I don't know how to get the program to recognise punctuation as a separate item in the list. 我正在编写一个程序,该程序通过使用数字序列复制文本来压缩文本-但我不知道如何使该程序将标点符号识别为列表中的单独项。

eg, in this sentence with a comma, the comma means that the words 'comma,' and 'comma' are different when using split() . 例如,在带逗号的句子中,逗号表示使用split()时单词'comma,''comma'是不同的。 I want to have 'comma' ',' 'comma' instead. 我想改用'comma' ',' 'comma'

I don't want to get rid of the punctuation - i want it as a separate item in a list 我不想删除标点符号-我希望将其作为列表中的单独项目使用

You can use re.split like this: 您可以这样使用re.split

>>> re.split('([{}])'.format(re.escape(string.punctuation)), "comma,comma")
['comma', ',', 'comma']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将句子字符串拆分为单词,还使标点符号成为一个单独的元素 - How to split a sentence string into words, but also make punctuation a separate element Python:如何使用正则表达式将句子拆分为新行,然后使用空格将标点符号与单词分开? - Python: How can I use a regex to split sentences to new lines, and then separate punctuation from words using whitespace? 如何从列表中的项目中删除标点符号并将其另存为列表中的单独项目? - How do I remove punctuation from an item in a list and save it as a separate item in the list? 使用 split() 在各种标点符号处分割字符串 - Dividing a string at various punctuation marks using split() 使用 Spacy 拆分单词内的多个标点符号 - Split on multiple punctuation inside a word using Spacy Python如何将标点符号与文本分开 - Python how to separate punctuation from text 如何使用 Python 将时间拆分为 10 分钟的时间间隔并将其作为单独的列 - How to split time into 10 min interval and make it as a separate column using Python 如何通过标点符号拆分 CSV 内容 - How to split CSV content by punctuation marks 如何通过标点符号拆分 pandas 列中的长字符串 - How to split long strings in pandas columns by punctuation 如何在评论中拆分没有标点符号的句子? - How to split sentences without punctuation end in a review?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM