Показать сообщение отдельно

  #1  
Старый 02.09.2024, 17:32
_Snake_
Познающий
Регистрация: 20.01.2022
Сообщений: 51
С нами: 2271859

Репутация: 8
По умолчанию

У меня код меняет структуру файла, а должен менять только prisec в файле.

Вот такая должна быть структура

Цитата:
Сообщение от Спойлер  

[{"count_maximum":0,"price":"8000","price_vc":10,"p osition_tab":1,"count":493,"enabled":true,"maximum ":false,"name":"Гражданский талон"}, и т.д
Код:





Код:
import
json
import
tkinter
as
tk
from
tkinter
import
filedialog
,
messagebox
import
chardet
def
detect_encoding
(
file_path
)
:
with
open
(
file_path
,
'rb'
)
as
file
:
raw_data
=
file
.
read
(
)
result
=
chardet
.
detect
(
raw_data
)
return
result
[
'encoding'
]
def
load_json_file
(
file_path
)
:
try
:
encoding
=
detect_encoding
(
file_path
)
with
open
(
file_path
,
'r'
,
encoding
=
encoding
)
as
file
:
return
json
.
load
(
file
)
except
json
.
JSONDecodeError
as
e
:
messagebox
.
showerror
(
"Ошибка"
,
f"Ошибка декодирования JSON:{e}"
)
return
None
except
Exception
as
e
:
messagebox
.
showerror
(
"Ошибка"
,
f"Ошибка при загрузке файла:{e}"
)
return
None
def
save_json_file
(
file_path
,
data
)
:
try
:
with
open
(
file_path
,
'w'
,
encoding
=
'utf-8'
)
as
file
:
json
.
dump
(
data
,
file
,
ensure_ascii
=
False
,
indent
=
4
)
messagebox
.
showinfo
(
"Успех"
,
f"Данные успешно сохранены в файл:{file_path}"
)
except
Exception
as
e
:
messagebox
.
showerror
(
"Ошибка"
,
f"Ошибка при сохранении файла:{e}"
)
def
update_prices
(
target_file_path
,
prices_file_path
)
:
target_data
=
load_json_file
(
target_file_path
)
if
target_data
is
None
:
return
prices_data
=
load_json_file
(
prices_file_path
)
if
prices_data
is
None
:
return
prices_dict
=
{
}
for
item_name
,
item_data
in
prices_data
[
'list'
]
.
items
(
)
:
if
'sa'
in
item_data
and
'price'
in
item_data
[
'sa'
]
:
prices_dict
[
item_name
]
=
item_data
[
'sa'
]
[
'price'
]
else
:
messagebox
.
showwarning
(
"Предупреждение"
,
f"Некорректная структура данных для товара '{item_name}' в файле с ценами."
)
for
item
in
target_data
:
if
item
[
'name'
]
in
prices_dict
:
item
[
'price'
]
=
prices_dict
[
item
[
'name'
]
]
save_json_file
(
target_file_path
,
target_data
)
def
select_target_file
(
)
:
file_path
=
filedialog
.
askopenfilename
(
title
=
"Выберите файл для обновления цен"
,
filetypes
=
[
(
"JSON files"
,
"*.json"
)
]
)
if
file_path
:
target_file_entry
.
delete
(
0
,
tk
.
END
)
target_file_entry
.
insert
(
0
,
file_path
)
def
select_prices_file
(
)
:
file_path
=
filedialog
.
askopenfilename
(
title
=
"Выберите файл с ценами"
,
filetypes
=
[
(
"JSON files"
,
"*.json"
)
]
)
if
file_path
:
prices_file_entry
.
delete
(
0
,
tk
.
END
)
prices_file_entry
.
insert
(
0
,
file_path
)
with
open
(
'prices_file_path.txt'
,
'w'
,
encoding
=
'utf-8'
)
as
file
:
file
.
write
(
file_path
)
def
on_update_click
(
)
:
target_file_path
=
target_file_entry
.
get
(
)
prices_file_path
=
prices_file_entry
.
get
(
)
if
target_file_path
and
prices_file_path
:
update_prices
(
target_file_path
,
prices_file_path
)
else
:
messagebox
.
showwarning
(
"Предупреждение"
,
"Пожалуйста, выберите оба файла."
)
def
load_prices_file_path
(
)
:
try
:
with
open
(
'prices_file_path.txt'
,
'r'
,
encoding
=
'utf-8'
)
as
file
:
prices_file_path
=
file
.
read
(
)
.
strip
(
)
if
prices_file_path
:
prices_file_entry
.
delete
(
0
,
tk
.
END
)
prices_file_entry
.
insert
(
0
,
prices_file_path
)
except
FileNotFoundError
:
pass
root
=
tk
.
Tk
(
)
root
.
title
(
"Обновление цен"
)
frame
=
tk
.
Frame
(
root
)
frame
.
pack
(
padx
=
10
,
pady
=
10
)
target_file_label
=
tk
.
Label
(
frame
,
text
=
"Выберите файл для обновления цен:"
)
target_file_label
.
grid
(
row
=
0
,
column
=
0
,
padx
=
5
,
pady
=
5
)
target_file_entry
=
tk
.
Entry
(
frame
,
width
=
50
)
target_file_entry
.
grid
(
row
=
0
,
column
=
1
,
padx
=
5
,
pady
=
5
)
target_file_button
=
tk
.
Button
(
frame
,
text
=
"Обзор"
,
command
=
select_target_file
)
target_file_button
.
grid
(
row
=
0
,
column
=
2
,
padx
=
5
,
pady
=
5
)
prices_file_label
=
tk
.
Label
(
frame
,
text
=
"Выберите файл с ценами:"
)
prices_file_label
.
grid
(
row
=
1
,
column
=
0
,
padx
=
5
,
pady
=
5
)
prices_file_entry
=
tk
.
Entry
(
frame
,
width
=
50
)
prices_file_entry
.
grid
(
row
=
1
,
column
=
1
,
padx
=
5
,
pady
=
5
)
prices_file_button
=
tk
.
Button
(
frame
,
text
=
"Обзор"
,
command
=
select_prices_file
)
prices_file_button
.
grid
(
row
=
1
,
column
=
2
,
padx
=
5
,
pady
=
5
)
update_button
=
tk
.
Button
(
frame
,
text
=
"Обновить цены"
,
command
=
on_update_click
)
update_button
.
grid
(
row
=
2
,
column
=
0
,
columnspan
=
3
,
pady
=
10
)
load_prices_file_path
(
)
root
.
mainloop
(
)
 
Ответить с цитированием