从Python中删除字符串的点标记时,请发出双引号的问题
我将其作为要更改的字符串 - line =''例如,如果您要计算数字列表的平均值,则 可以检查结果是否不大于列表中最大元素 或小于最小的。这称为“理智检查”,因为它检测到 结果“完全不合逻辑”。'''
并使用以下代码 - 导入字符串 out = line.translate(line.maketrans(“”,“”,string.punctuation))
我得到的输出 '例如,如果您要计算数字列表的平均值,则应检查结果是否不大于列表\ shore d的最大元素,也不比最小的元素大,因为它检测到\ nResults,因此称为“理智检查”这是“完全不合逻辑的”'
双引号仍然存在。如何删除双引号?
im giving this as the string to be changed-
line=''' For example, if you are computing the average of a list of numbers, you
could check that the result is not greater than the largest element in the list
or less than the smallest. This is called a “sanity check” because it detects
results that are “completely illogical”.'''
and using the following code-
import string
out = line.translate(line.maketrans("", "", string.punctuation))
the output I get is-
' For example if you are computing the average of a list of numbers you\ncould check that the result is not greater than the largest element in the list\nor less than the smallest This is called a “sanity check” because it detects\nresults that are “completely illogical”'
The double quotes are still there. How to remove double quotes as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定翻译功能对字符串有什么作用,从来没有真正与它们一起玩过。但是,如果您要完成的一切都是删除双引号,那么:
line.replace('“'','')
这将替换句子中的所有双引号,从而有效地删除它们。
I'm not sure exactly what the translate functions do to strings, never really played with them.. but if all you're trying to accomplish is removing double quotes then:
line.replace('"','')
this will replace all double quotes in the sentence with nothing, effectively deleting them.