[Answer]-Need some help in deleting the data from list and again append it in same list

1πŸ‘

βœ…

Use list-expression to filter the splitted text, and rebuild the string using join function

>>> db_path = 'FileStore/client/Logging a Defect.docx,FileStore/client/Mocks (1).pptx,FileStore/client/Mocksv2.pptx'
>>> file_to_delete = 'Logging a Defect.docx'
>>> file_separator = ","
>>> new_db_path = [
...     path.strip()
...     for path in db_path.split(file_separator)
...     if path.strip() and file_to_delete not in path
... ]
>>> string_to_save = file_separator.join(new_db_path)
>>> string_to_save
'FileStore/client/Mocks (1).pptx,FileStore/client/Mocksv2.pptx'
πŸ‘€xecgr

0πŸ‘

You can read the text in your database and then use remove method of the list in python and then write back the new value into databse:

text = "FileStore/client/Logging a Defect.docx,FileStore/client/Mocks (1).pptx,FileStore/client/Mocksv2.pptx,"
splitted = text.split(',')
#filename is the one you want to delete
entry = "FileStore/client/{filename}".format(filename="Mocks (1).pptx")
if entry in splitted:
    splitted.remove(entry)

newtext = ""
for s in splitted:
    newtext += s
    newtext += ','

now write back newtext to database

πŸ‘€mehdix

0πŸ‘

Not boasting or anything but I came up with my own logic for my question. It looks far less complicated but it works fine.

                db_path = 'FileStore/client/Logging a Defect.docx,FileStore/client/Mocks (1).pptx,FileStore/client/Mocksv2.pptx'
                path_list = db_path.split(",")
                doc = 'Logging a Defect.docx'
                for i in path_list :
                    if doc in i:
                        y.remove("FileStore/"+client+"/"+doc)
                new_path =  ",".join(y)
                print new_path
πŸ‘€d-coder

Leave a comment