L
L
lemonlimelike2020-07-06 16:44:01
Python
lemonlimelike, 2020-07-06 16:44:01

How to make changes to the list?

Hello! Developed a graphical interface using pysimplegui. Some lines from the list are written to the window (type textarea) for text, each line from a new line. These lines should be changed in the future and more text should be added to them, but I can’t implement this. Help

Here is the code that relates to my problem.

list_default_alias = get_list_alias(values['file_input'])
str_alias = '\n'.join([item['brand'] for item in list_default_alias])
if list_default_alias:
    child_layout = [
        [sg.Multiline(default_text=str_alias,
                      key='list_alias', size=(40, 30))],
        [sg.Button(button_text='Сохранить изменения',
                   enable_events=True, key='Save_Change_Alias')]
    ]
    child_window = sg.Window('Alias', child_layout)

    while True:
        child_event, child_values = child_window.read(timeout=1000)
        if child_event in (None, 'Exit', 'Cancel'):
            break

        if child_event == 'Save_Change_Alias':
            '''Отлавливания события нажатия на кнопку'''
            list_values_alias = child_values['list_alias'].split('\n') # превращаем текст, который в окошке в список. Делаю так и тут, потому что этот список изменяется и мне нужно сохранить уже измененный список в файл
            with open('alias', 'r+') as file_alias:
                list_alias_file = [] # Достаю из файла все строки. Они нужны для проверки на дубликаты, чтоб не добавлять одинаковые записи
                for item in file_alias:
                    list_alias_file.append(
                        item.replace('\n', '').lower())
                '''Тут должна быть логика добавления уже измененного списка. Тут у меня и проблемы'''
                print(list_alias_file)
                for alias in list_values_alias:
                    for alias_file in list_alias_file:
                        if alias.replace('\n', '').lower() in alias_file:
                            file_alias.write(f'{alias_file},{alias}')

                    # if alias.replace('\n','').lower() not in list_alias_file:
                        # file_alias.write(alias+'\n')

    child_window.close()


Function:

def get_list_alias(input_file_name):
    '''Получение списка товаров из файла'''
    if not input_file_name:
        return []
    list_data = []
    wb = load_workbook(filename=input_file_name)
    sheet = wb.active
    m_row = sheet.max_row

    for i in range(1, m_row+1):
        if not sheet.cell(row=i, column=1).value:
            continue
        brand = str(sheet.cell(row=i, column=2).value).replace(
            "'", '').title()
        object = {'brand': brand}
        if object not in list_data:
            list_data.append(object)

    return list_data


An example of a list that comes from a function in list_default_alias:
[{'brand': 'Bosch'}, {'brand': 'Bsg'}, {'brand': 'Contitech'}, {'brand': 'Dayco'}, {'brand': 'Delphi'}, {'brand': 'Denso'}, {'brand': 'Elring'}, {'brand': 'Gates '}, {'brand': 'General Motors'}]


And a picture of what the list looks like after the changes.
5f032a868c3e7676428157.png

An example of how the list should look before and after the changes:
Before, it's like on the screenshot. And after this:
Bosch
Bsg
Contitech
Dayco
Delphi
Denso
Elring
Gates,Gat
General Motors,General+Motors


Etc. That is, I need to be able to change the list and save already changed lines to the same lines. Thank you very much in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vadim, 2020-07-06
@Viji

l_len=len(list_default_alias) -- list length
object = {'brand': "General Motors,General+Motors"}
list_default_alias[n] = object

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question