Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how do i convert ['121341']['132324'] (type string) into 2 separate lists python

i am trying to add some file operation capabilities to a example program, i am strugling with the reading from the file. here is the code that is modified.

def read(fn): 
    fileout=open(f"{fn}","a+")
    fileout.seek(0,0)
    s=fileout.readlines()
    if s==[]:
        print("the file specified does not appear to exists or is empty. if the file does not exist, it will be created")
    else:
        last=s[-1]
        print(last)
        print(type(last))
        convert(last)

def find(last):
    tup=last.partition(".")
    fi=tup[0:1]
    return fi[0]
def convert(last):
    tup=last.partition(".")
    part=tup[2:]
    print(part)
    part=part[0]
    print(part)
    part=part.split("\n")
    print(part)
    part=part[0]
    print(part)
    print(type(part))
#__main__
file(fn)

the write functionality writes in the form of
(fileindex number).[(planned campaign)][(conducted campaign)]
example:- some random data writen to the file by the program(first two number are dates)

0.['12hell']['12hh']
1.['12hell']['12hh']
2.['121341']['132324']

but i am strugling to write the read function, i don’t understand how i could convert the data back.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

with the current read function i get back

['121341']['132324']

as a string type, i have brainstormed many ideas but could not figureout how to convert string to list(they need to be 2 separate lists)

>Solution :

Insert a ',' inbetween the brackets, then use eval. This will return a tuple of lists.

strLists = "['121341']['132324']['abcdf']"
strLists = strLists.replace('][', '],[')

evalLists = eval(strLists)


for each in evalLists:
    print(each)

Output:

['121341']
['132324']
['abcdf']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading