如何让这个程序忽略标点符号

时间:2016-09-14 17:52:00

标签: python

我是python的新手,我不知道如何让这个程序忽略标点符号;我知道这真的是效率低下但是我现在并没有为此烦恼。

var mockData = {
"profile": [{
    "name": "Mario",
    "city": "Los Angeles",
    "state": "California",
    "gameOwned": "Grand Theft Auto 5 (PS4)",
    "gameWanted": "Battlefield 4 (PS4)",
    "publishedAt": new Date()
}, {
    "name": "Colin",
    "city": "Los Angeles",
    "state": "California",
    "gameOwned": "Battlefield 4 (PS4)",
    "gameWanted": "Grand Theft Auto 5 (PS4)",
    "publishedAt": new Date()
}],
"city": ["Los Angeles", "New York"],
    "game": ["Battlefield 4 (PS4)", "Grand Theft Auto 5 (PS4)"]
}

我感谢你能给我的任何帮助

1 个答案:

答案 0 :(得分:1)

您可以使用Python的string模块来帮助测试标点符号。

>> import string
>> print string.punctuation
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
>> sentence = "I am a sentence, and; I haven't been punctuated well.!"

你可以split每个空格的句子来从你的句子中获取单个单词,然后从每个单词中删除标点符号。或者,您可以先从句子中删除标点符号,然后重新构成单个单词。我们将做选项2 - 列出句子中的所有字符,标点符号除外,然后将该列表连接在一起。

>> cleaned_sentence = ''.join([c for c in sentence if c not in string.punctuation])
>> print cleaned_sentence
'I am a sentence and I havent been punctuated well'

请注意,“没有”的撇号被删除了 - 完全忽略标点符号的副作用。