替换JSON文件中的阿拉伯字符



我想删除这个JSON文件中的所有阿拉伯字符:

comments.json

{
"comments": [
"Dunkin Doughnuts inside a bowling center? That's really amazing 👍🏼",
"Best localtion",
"Dunkin' Donuts☕️👌🏼",
"Good",
"Tasty",
"Donuts",
"Dunkin' Donuts DD The well-known does not know My order was espresso The branch is located inside the bowling alley But the fault of this branch is that one employee is late In orders and their donuts are second to none 🧡💛 دانكن دونتس DD المعروف لا يعرف طلبي كان اسبريسو الفرع موجود داخل صالة البولينق لكن عيب هالفرع ان موظف واحد ويتأخر في الطلبات والدونات عندهم لا يعلى عليها 🧡💛",
"Dunkin 'Donuts is a global coffee chain, which has been in operation since 1950 in Quincy, USA. William Rosberg opened a restaurant selling coffee and donuts in 1948, in Quincy (Massachusetts) and named it The Open Kettle, meaning an open pan. After the project was a success, and after a while he opened the first coffee and donut shop in 1950, he was known as Dunkin Donuts and it is still standing to this day. In 1954 Rosenburg opened five more Dunkin 'Donuts stores with some other products added to the order list. In 1980, Dunkin Donuts bought Baskin Robbins and some other companies to establish a coalition company called the Dunkin 'Brands brand, and then it spread all over the world. دانكن دونتس (بالإنجليزية: Dunkin' Donuts)سلسلة محلات قهوة عالمية، بدأت منذ 1950 في كوينسي بالولايات المتحدة الأمريكية. قام ويليام روزبيرغ بإفتتاح مطعم يبيع القهوة والدونات عام 1948، في كوينسي (ماساتشوستس) وأطلق عليه اسم The Open Kettle أي المقلاة المفتوحة. بعد أن لاقى المشروع النجاح، وبعد فترة قام بافتتاح أول محل للقهوة والدونت عام 1950، عرف باسم دانكن دونتس حيث ما زال قائما حتى يومنا هذا. وفي 1954 افتتح روزنبورق خمسة فروع أخرى من محلات دانكن دونتس مع اضافة بعض المنتجات الأخرى إلى قائمة الطلبات. في 1980 اشترت شركة دانكن دونتس باسكن روبنز وبعض الشركات الأخرى لتأسيس شركه تحالفيه تسمى علامة دانكن Dunkin' Brands وانتشرت بعدها في جميع أنحاء العالم.",
"A solution is not alive حلا ولا احلي",
"excellent ممتاز",
"How many number arrived home? كم رقم توصل للبيت؟",
"Dunkin is inside the bowling alley دانكن داخل صالة البولنج",
"What it does ماعليه",
"A solution is not alive حلا ولا احلي",
"I'm a Dunkin fan انا من عشاق دنكن"
]
}

我试图用下面的代码删除它,我想打印它没有阿拉伯字符。deleted_chars包含我要删除的所有字母。

import json
comments = []
comments_after_deletion = {'comments': []}
deleted_char_arr = "دجحخهعغفقثصضطكمنتالبيسشظزوةىلارؤءئذ؛×÷‘إلإًٌَُّ،ـألأٍِ؟آلآْ"
with open("reviews V1.0.json", 'r', encoding="utf-8") as jsn:
coms = json.load(jsn)
for comment in coms["comments"]:
for letter in comment:
for deleted_char in deleted_char_arr:
if letter == deleted_char:
comment.replace(letter, '')
comments_after_deletion['comments'].append(comment)
with open("comments after deletion.json", 'w', encoding="utf-8") as f:
json.dump(comments_after_deletion, f, ensure_ascii=False, indent=4)

这是我得到的新JSON文件:

delete .json后的注释

{
"comments": [
"Dunkin Doughnuts inside a bowling center? That's really amazing 👍🏼",
"Best localtion",
"Dunkin' Donuts☕️👌🏼",
"Good",
"Tasty",
"Donuts",
"Dunkin' Donuts DD The well-known does not know My order was espresso The branch is located inside the bowling alley But the fault of this branch is that one employee is late In orders and their donuts are second to none 🧡💛 دانكن دونتس DD المعروف لا يعرف طلبي كان اسبريسو الفرع موجود داخل صالة البولينق لكن عيب هالفرع ان موظف واحد ويتأخر في الطلبات والدونات عندهم لا يعلى عليها 🧡💛",
"Dunkin 'Donuts is a global coffee chain, which has been in operation since 1950 in Quincy, USA. William Rosberg opened a restaurant selling coffee and donuts in 1948, in Quincy (Massachusetts) and named it The Open Kettle, meaning an open pan. After the project was a success, and after a while he opened the first coffee and donut shop in 1950, he was known as Dunkin Donuts and it is still standing to this day. In 1954 Rosenburg opened five more Dunkin 'Donuts stores with some other products added to the order list. In 1980, Dunkin Donuts bought Baskin Robbins and some other companies to establish a coalition company called the Dunkin 'Brands brand, and then it spread all over the world. دانكن دونتس (بالإنجليزية: Dunkin' Donuts)سلسلة محلات قهوة عالمية، بدأت منذ 1950 في كوينسي بالولايات المتحدة الأمريكية. قام ويليام روزبيرغ بإفتتاح مطعم يبيع القهوة والدونات عام 1948، في كوينسي (ماساتشوستس) وأطلق عليه اسم The Open Kettle أي المقلاة المفتوحة. بعد أن لاقى المشروع النجاح، وبعد فترة قام بافتتاح أول محل للقهوة والدونت عام 1950، عرف باسم دانكن دونتس حيث ما زال قائما حتى يومنا هذا. وفي 1954 افتتح روزنبورق خمسة فروع أخرى من محلات دانكن دونتس مع اضافة بعض المنتجات الأخرى إلى قائمة الطلبات. في 1980 اشترت شركة دانكن دونتس باسكن روبنز وبعض الشركات الأخرى لتأسيس شركه تحالفيه تسمى علامة دانكن Dunkin' Brands وانتشرت بعدها في جميع أنحاء العالم.",
"A solution is not alive حلا ولا احلي",
"excellent ممتاز",
"How many number arrived home? كم رقم توصل للبيت؟",
"Dunkin is inside the bowling alley دانكن داخل صالة البولنج",
"What it does ماعليه",
"A solution is not alive حلا ولا احلي",
"I'm a Dunkin fan انا من عشاق دنكن"
]
}

它不工作,为什么有人知道如何解决它吗?

最小的变化是改变行

comment.replace(letter, '')

comment = comment.replace(letter, '')

然而,这仍然是相当低效的。考虑一次删除整个不需要的字符集。

在你的代码开头只放一次:

translation_table = str.maketrans('', '', deleted_char_arr)

…然后你可以输入:

comment = comment.translate(translation_table)

…一次执行所有的删除。


参见工作示例https://replit.com/@CharlesDuffy2/KindheartedRareBrowsers#main.py;复制到这里:

import json
comments = []
c_a_d = {'comments': []} # comments_after_deletion, shortened
deleted_char_arr = "دجحخهعغفقثصضطكمنتالبيسشظزوةىلارؤءئذ؛×÷‘إلإًٌَُّ،ـألأٍِ؟آلآْ"
translation_table = str.maketrans('', '', deleted_char_arr)
with open("reviews V1.0.json", 'r', encoding="utf-8") as jsn:
coms = json.load(jsn)
for comment in coms["comments"]:
c_a_d['comments'].append(comment.translate(translation_table))
with open("comments after deletion.json", 'w', encoding="utf-8") as f:
json.dump(c_a_d, f, ensure_ascii=False, indent=4)

最新更新