gpt4 book ai didi

python - 从字符串中删除所有多行垃圾?

转载 作者:行者123 更新时间:2023-12-01 01:49:26 26 4
gpt4 key购买 nike

我想从字符串中删除所有与 Markdown 相关的内容。

这是一个例子:

>EU

>Please spread this like the plague.

🤔

这是我想要的单行输出:EU。请像瘟疫一样传播它。 🤔

知道如何做到这一点吗?

我已经尝试过 .rstrip() 和 `.replace('\n', ''),尽管它没有做任何事情。

也许我应该将字符串转换为 html markdown,然后使用可用的实用程序将其删除? (剧透:我不知道如何将字符串转换为 markdown html)

我还需要将其写入 csv。这是我之前发布的示例(同时应用了 rstripreplace)。

>EU\
\
>Please spread this like the plague.\
\
<emoticon>

最佳答案

s = """>EU
>Please spread this like the plague.
🤔"""

print(' '.join(s.replace('>','').replace('.','').replace('\n','.').split()))

Out[ ]:
EU. Please spread this like the plague. 🤔

请注意,这需要我手动删除空行。

另一种方法是使用正则表达式。

import re
s = re.sub(r">", '', s)
s = re.sub(r" {2}", '', s)
s = re.sub(r"\.", '', s)
s = re.sub(r"\n\n", '.', s)
s = re.sub(r" \n", '', s)
s = re.sub(r"\n", '.', s)
print(s)

Out[ ]:
EU. Please spread this like the plague. 🤔

关于python - 从字符串中删除所有多行垃圾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50879291/

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