【问题标题】:Remove Puncuation From JSON File Only Inside Quotation Marks仅在引号内从 JSON 文件中删除标点符号
【发布时间】:2019-11-06 15:39:51
【问题描述】:

我有多个 JSON 文件,其中包含最多数百行的字符串。我的文件示例中只有三行,但平均而言,这些“短语”大约有 200-500 个:

{
   "version": 1,
   "data": {
       "phrases":[
           "A few words that's it.",
           "This one, has a comma in it!",
           "hyphenated-sentence example"
        ]
   }
}

我需要有一个脚本进入文件(我们可以称之为 ExampleData.json)并删除所有标点符号(特别是这些字符:,.?!'- 从文件中,而不删除双引号之外的 ,标记。基本上是这样:

"A few words that's it.",
"This one, has a comma in it!",
"hyphenated-sentence example."

变成这样:

"A few words that's it",
"This one has a comma in it",
"hyphenated sentence example"

还要注意除连字符之外的所有标点符号是如何被删除的。那被一个空格代替。


我发现了一个与此类似的几乎相同的问题,但对于 csv 文件here,但无法将 csv 版本转换为适用于 JSON 的内容。

我与 python 最接近的是通过someone else's answer 在另一个线程上使用字符串。

input_str = 'please, remove all the commas between quotes,"like in here, here, here!"'

quotes = False

def noCommas(string):
    quotes = False
    output = ''
    for char in string:
        if char == '"':
            quotes = True
        if quotes == False:
            output += char
        if char != ',' and quotes == True:
            output += char
    return output

print noCommas(input_str)

(抱歉,我不知道如何将代码块放在引号中)
但它一次只适用于一个角色。但是通过添加任何额外的规则会导致引号之外的文本自身加倍(请变为 pplleeaassee)。
最后一件事是我必须在 python2.7.5 中执行此操作,根据我汇总的搜索结果,这使得这变得更加困难。
很抱歉,我对 python 还是这么陌生,必须马上做一些不平凡的事情,但这不是我的选择。

【问题讨论】:

  • 尝试将您的 json 加载为 dict,然后使用 re.substr.translate 处理您的字符串以删除不需要的字符,正如此答案所建议的 (stackoverflow.com/a/3939381/8053370),然后将其再次保存到您的文件中。
  • 我已经能够以我的方式通过大部分逻辑。我将open( .json 文件作为fin 并将data = fin.read() data = data.replace('?','') 应用于我所有适用的字符,逗号除外。剩下的就是弄清楚如何确定逗号是否在双引号内。我能想到的方法是:如果逗号在\n 旁边,如果它在双引号旁边,或者如果它位于两个引号内。仍然不知道其中一条或另一条路线是否是更好的选择。

标签: python json python-2.7


【解决方案1】:

这应该可行。

import re
import json

with open('C:/test/data.json') as json_file:
    data = json.load(json_file)



for idx, v in enumerate(data['data']['phrases']):
    data['data']['phrases'][idx] = re.sub(r'-',' ',data['data']['phrases'][idx])
    data['data']['phrases'][idx] = re.sub(r'[^\w\s]','',data['data']['phrases'][idx])


with open('C:/test/data.json', 'w') as outfile:
    json.dump(data, outfile,  indent=4)

选项 2:

将 json 作为字符串加载。然后使用正则表达式查找双引号之间的所有子字符串。从所有这些子字符串中替换/去除标点符号,然后写回文件:

import re
import json
import string




with open('C:/test/data.json') as json_file:
    data = json.load(json_file)

data = json.dumps(data)

strings = re.findall(r'"([^"]*)"', data)

for each in strings:
    new_str =  re.sub(r'-',' ', each)
    new_str = new_str.strip(string.punctuation)
    new_str =  re.sub(r',','', new_str)

    data = data.replace('"%s"' %each, '"%s"' %new_str)


with open('C:/test/data_output.json', 'w') as outfile:
    json.dump(json.loads(data), outfile,  indent=4)

【讨论】:

  • 对不起,我应该在 OP 中指定这些是单独的 JSON 文件,最多可以包含数百行字符串。所以我不会在 python 脚本中拥有实际的 JSON,而是作为我正在编辑的单个文件。我会更新我的帖子以更好地反映这一点。
  • 嗯,好的。明天早上我会用一个应该可行的解决方案来看看它。只是澄清一下,这些短语都通通了吗?您基本上希望从所有值中删除标点符号?不管它的键是什么?
  • 是的。所以这三个示例短语,假设有大约 500 个。这就是整个文件。另外我应该注意,在我的 .json 文件中,{ 之前的 data = 不存在。如果可以的话,我会更改它,但我不是生成这些文件的人。
  • @chitown88 imo,您的第一个选项 1 很好,您可以像在选项 2 中一样加载 json,仅此而已。
  • @VictorGalisson,我同意(我在 cmets 中提到过,但实际上只是在解决方案中对其进行了编辑).. 但我开始考虑是否有嵌套值,或者 json没有专门将 "phrases" 作为键。在不确切知道 json 的样子的情况下,我只是想提供一种更强大的方法。我知道它仍然存在缺陷,但至少提供了另一种选择
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-26
  • 2013-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
相关资源
最近更新 更多