Я разрабатываю программу, которая будет читать текстовые файлы и динамически конвертировать их в формат JSON. Пользователи могут ввести имя ключа и соответствующую цифру, чтобы вырезать индекс из необработанного текстового файла. При чтении файла небольшого размера все работает нормально, но всякий раз, когда текстовые файлы огромны, программа загружается в течение нескольких минут, чтобы продолжить. Я новичок в программировании и у меня нет опыта, чтобы сделать код более «эффективным». Моя цель — сделать цикл (чтение файла) более быстрым. Кто-нибудь может мне помочь? Заранее спасибо.
Мои огромные текстовые файлы составляют около 200 КБ и более 2000 строк журнала.
После того, как пользователь нажмет «Просмотреть JSON», программа преобразует текстовый файл в формат json на основе фрагментов индекса и распечатает его в терминале (именно здесь процесс, который я хочу ускорить)
Мой код:
#Review JSON format
def ReviewJson():
#if no selection is choosed
if x == 183.0:
tkMessageBox.showinfo("Error", "You must select a file before can review.", icon="error")
else:
# ========valdiate buttons for create new profile
try:
ReviewJSON_button.configure(state=DISABLED)
# Delete submit button when select existing value
CreateJsonBtn.destroy()
CreateNewJsonBtn.configure(state=NORMAL)
except:
pass
global reviewjson, window
window = Toplevel(root)
window.minsize(800, 600)
window.maxsize(800, 600)
window.title("Foghorn Publisher - Review Json" + " " + "(" + options.get() + ")")
window.protocol('WM_DELETE_WINDOW', destroyReviewJSON)
reviewjson = Text(window, bg='black', foreground="white", height="20", width="500")
reviewjson.pack()
reviewjson.config(state=NORMAL)
reviewjson.delete("1.0", "end")
file_data.clear()
try:
global datalist,cleandatalist
datalist=[]
for content in data2:
#Key1name cant be empty
if (key1EntryName.get()==""):
window.destroy()
ReviewJSON_button.configure(state=NORMAL)
tkMessageBox.showinfo("Error","First key name cannot be empty",icon="error")
#start index and end index cannot be empty after name is been declared
elif(key1EntryName.get() != 0) and (key1EntryStartIndex.get() =="") and (key1EntryEndIndex.get() ==""):
window.destroy()
ReviewJSON_button.configure(state=NORMAL)
tkMessageBox.showinfo("Error", "Start index and end index cannot be empty after declaration of key name", icon="error")
# check 1: check start to EOS
elif (key1EntryEndIndex.get()) == "":
file_data[key1EntryName.get()] = content[int(key1EntryStartIndex.get()):]
# check 1: check EOS to start
elif (key1EntryStartIndex.get()) == "":
file_data[key1EntryName.get()] = content[:int(key1EntryEndIndex.get())]
# check 1: normal status
else:
file_data[key1EntryName.get()] = content[int(key1EntryStartIndex.get()):int(key1EntryEndIndex.get())]
######################Check 2 ################################
#check 2: If all empty jiu dont call this part
if(key2EntryName.get() or key2EntryStartIndex.get() or key2EntryEndIndex.get()) == "":
pass
#check 2: start index and end index cannot be empty after name is been declared
elif(key2EntryName.get() != 0) and (key2EntryStartIndex.get() =="") and (key2EntryEndIndex.get() ==""):
window.destroy()
ReviewJSON_button.configure(state=NORMAL)
tkMessageBox.showinfo("Error", "Start index and end index cannot be empty after declaration of key name", icon="error")
elif (key2EntryName.get() == "") and (key2EntryStartIndex.get() != 0) and (
key2EntryEndIndex.get() == ""):
window.destroy()
ReviewJSON_button.configure(state=NORMAL)
tkMessageBox.showinfo("Error", "Key name cannot be empty after declaration of start index",
icon="error")
elif (key2EntryName.get() == "") and (key2EntryStartIndex.get() == "") and (
key2EntryEndIndex.get() != 0):
window.destroy()
ReviewJSON_button.configure(state=NORMAL)
tkMessageBox.showinfo("Error", "Key name cannot be empty after declaration of end index",
icon="error")
elif (key2EntryName.get() == "") and (key2EntryStartIndex.get() != 0) and (
key2EntryEndIndex.get() != 0):
window.destroy()
ReviewJSON_button.configure(state=NORMAL)
tkMessageBox.showinfo("Error", "Key name cannot be empty after declaration of start & end index", icon="error")
# check 2: check start to EOS
elif (key2EntryEndIndex.get()) == "":
file_data[key2EntryName.get()] = content[int(key2EntryStartIndex.get()):]
# check 2: check EOS to start
elif (key2EntryStartIndex.get()) == "":
file_data[key2EntryName.get()] = content[:int(key2EntryEndIndex.get())]
# check 2: normal status
else:
file_data[key2EntryName.get()] = content[int(key2EntryStartIndex.get()):int(key2EntryEndIndex.get())]
######################Check 3 ################################
# check 3: If all empty jiu dont call this part
if (key3EntryName.get() or key3EntryStartIndex.get() or key3EntryEndIndex.get()) == "":
pass
# check 3: start index and end index cannot be empty after name is been declared
elif (key3EntryName.get() != 0) and (key3EntryStartIndex.get() == "") and (
key3EntryEndIndex.get() == ""):
window.destroy()
ReviewJSON_button.configure(state=NORMAL)
tkMessageBox.showinfo("Error",
"Start index and end index cannot be empty after declaration of key name",
icon="error")
elif (key3EntryName.get() == "") and (key3EntryStartIndex.get() != 0) and (
key3EntryEndIndex.get() == ""):
window.destroy()
ReviewJSON_button.configure(state=NORMAL)
tkMessageBox.showinfo("Error", "Key name cannot be empty after declaration of start index",
icon="error")
elif (key3EntryName.get() == "") and (key3EntryStartIndex.get() == "") and (
key3EntryEndIndex.get() != 0):
window.destroy()
ReviewJSON_button.configure(state=NORMAL)
tkMessageBox.showinfo("Error", "Key name cannot be empty after declaration of end index",
icon="error")
elif (key3EntryName.get() == "") and (key3EntryStartIndex.get() != 0) and (
key3EntryEndIndex.get() != 0):
window.destroy()
ReviewJSON_button.configure(state=NORMAL)
tkMessageBox.showinfo("Error",
"Key name cannot be empty after declaration of start & end index",
icon="error")
# check 3: check start to EOS
elif (key3EntryEndIndex.get()) == "":
file_data[key3EntryName.get()] = content[int(key3EntryStartIndex.get()):]
# check 3: check EOS to start
elif (key3EntryStartIndex.get()) == "":
file_data[key3EntryName.get()] = content[:int(key3EntryEndIndex.get())]
# check 3: normal status
else:
file_data[key3EntryName.get()] = content[
int(key3EntryStartIndex.get()):int(key3EntryEndIndex.get())]
######################Check 4 ################################
# check 4: If all empty jiu dont call this part
if (key4EntryName.get() or key4EntryStartIndex.get() or key4EntryEndIndex.get()) == "":
pass
# check 4: start index and end index cannot be empty after name is been declared
elif (key4EntryName.get() != 0) and (key4EntryStartIndex.get() == "") and (
key4EntryEndIndex.get() == ""):
window.destroy()
ReviewJSON_button.configure(state=NORMAL)
tkMessageBox.showinfo("Error",
"Start index and end index cannot be empty after declaration of key name",
icon="error")
elif (key4EntryName.get() == "") and (key4EntryStartIndex.get() != 0) and (
key4EntryEndIndex.get() == ""):
window.destroy()
ReviewJSON_button.configure(state=NORMAL)
tkMessageBox.showinfo("Error", "Key name cannot be empty after declaration of start index",
icon="error")
elif (key4EntryName.get() == "") and (key4EntryStartIndex.get() == "") and (
key2EntryEndIndex.get() != 0):
window.destroy()
ReviewJSON_button.configure(state=NORMAL)
tkMessageBox.showinfo("Error", "Key name cannot be empty after declaration of end index",
icon="error")
elif (key4EntryName.get() == "") and (key4EntryStartIndex.get() != 0) and (
key4EntryEndIndex.get() != 0):
window.destroy()
ReviewJSON_button.configure(state=NORMAL)
tkMessageBox.showinfo("Error",
"Key name cannot be empty after declaration of start & end index",
icon="error")
# check 4: check start to EOS
elif (key4EntryEndIndex.get()) == "":
file_data[key4EntryName.get()] = content[int(key4EntryStartIndex.get()):]
# check 4: check EOS to start
elif (key4EntryStartIndex.get()) == "":
file_data[key4EntryName.get()] = content[:int(key4EntryEndIndex.get())]
# check 4: normal status
else:
file_data[key4EntryName.get()] = content[int(key4EntryStartIndex.get()):int(
key4EntryEndIndex.get())]
######################Check 5 ################################
# check 5: If all empty jiu dont call this part
if (key5EntryName.get() or key5EntryStartIndex.get() or key5EntryEndIndex.get()) == "":
pass
# check 5: start index and end index cannot be empty after name is been declared
elif (key5EntryName.get() != 0) and (key5EntryStartIndex.get() == "") and (
key5EntryEndIndex.get() == ""):
window.destroy()
ReviewJSON_button.configure(state=NORMAL)
tkMessageBox.showinfo("Error",
"Start index and end index cannot be empty after declaration of key name",
icon="error")
elif (key5EntryName.get() == "") and (key5EntryStartIndex.get() != 0) and (
key5EntryEndIndex.get() == ""):
window.destroy()
ReviewJSON_button.configure(state=NORMAL)
tkMessageBox.showinfo("Error",
"Key name cannot be empty after declaration of start index",
icon="error")
elif (key5EntryName.get() == "") and (key5EntryStartIndex.get() == "") and (
key5EntryEndIndex.get() != 0):
window.destroy()
ReviewJSON_button.configure(state=NORMAL)
tkMessageBox.showinfo("Error",
"Key name cannot be empty after declaration of end index",
icon="error")
elif (key5EntryName.get() == "") and (key5EntryStartIndex.get() != 0) and (
key5EntryEndIndex.get() != 0):
window.destroy()
ReviewJSON_button.configure(state=NORMAL)
tkMessageBox.showinfo("Error",
"Key name cannot be empty after declaration of start & end index",
icon="error")
# check 5: check start to EOS
elif (key5EntryEndIndex.get()) == "":
file_data[key5EntryName.get()] = content[int(key5EntryStartIndex.get()):]
# check 5: check EOS to start
elif (key5EntryStartIndex.get()) == "":
file_data[key5EntryName.get()] = content[:int(key5EntryEndIndex.get())]
# check 5: normal status
else:
file_data[key5EntryName.get()] = content[int(key5EntryStartIndex.get()):int(
key5EntryEndIndex.get())]
# output to JSON
global tmp
tmp = json.dumps(file_data, ensure_ascii=False, indent="t")
datalist.append(tmp)
# We want to strip all elements in this list
clearslashN = [i.replace('n','') for i in datalist]
cleandatalist = [i.replace('t', '') for i in clearslashN]
print(cleandatalist)
except:
raise
```
1 ответ
Имена ключей, а также начальные и конечные индексы никогда не меняются. Нет смысла тестировать их на каждой итерации. Переместите все фрагменты проверки ошибок за пределы цикла.
То же самое относится к
contentнарезка. Заранее вычислите границы.Первая строка вашего файла данных выровнена по-другому. Бьюсь об заклад, его json неверен.
Вместо нарезки
contentв столбцах используйте регулярное выражение, напримерr'([^ ]+) ([^;]+);([^ ]+) ([^ ]+) (.*)$'Не снимайте всю
datalistснова и снова. Только полосаtmp, прежде чем добавить его в список данных.

