gpt4 book ai didi

python - 如何从python中的字符串中搜索换行符

转载 作者:行者123 更新时间:2023-11-28 22:33:59 24 4
gpt4 key购买 nike

我想根据换行符拆分字符串并将新行替换为“.”在 python 。我试过这段代码,但我不明白。

import nltk
from nltk.tokenize.punkt import PunktSentenceTokenizer, PunktParameters, PunktLanguageVars
ex_sent="Conclusion & Future Work
In our project we have tried for implementing northbound SDN application for OpenFlow protocol evaluation. The client and the RYU application is being connected, via socket connection."
class CommaPoint(PunktLanguageVars):
sent_end_chars = (',','\n')
tokenizer = PunktSentenceTokenizer(lang_vars = CommaPoint())
sentences = sentence_splitter.tokenize(ex_sent)
print sentences

最佳答案

换行符是\n。所以如果你想用 . 替换字符串中的所有新行字符,你应该使用 replace() 方法。在这种情况下:

your_string.replace('\n', '.')

第二种方法是 .split('\n') 按换行符然后 '.'.join(your_string)。你应该检查 Python documentation.

关于python - 如何从python中的字符串中搜索换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39345915/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com