Python Dictionary Split Questions

Asked 2 years ago, Updated 2 years ago, 91 views

Hello! Movie_data.txt is filled with data such as movie name, company, release date, etc., and I called this notepad and separated it by '|' and saved it in a dictionary called mydict.

mydict = {}

fp=open("drive/MyDrive/pythontest/movie_data.txt", 'r', encoding='cp949')

for line in fp.readlines():
    line = line.strip()
    x= line.split("|")
    mydict[x[0]] =x[1]
    mydict[x[1]] =x[2]
    mydict[x[2]] =x[3]
    mydict[x[3]] =x[4]
    mydict[x[4]] =x[5]
    mydict[x[5]] =x[6]
    mydict[x[6]] =x[7]
    mydict[x[7]] =x[8]



fp.close()



while True:
    in_word = input ("Enter movie name (exit:0)")
    x=in_word

    if (in_word =='0'):
        break

    else:
        if (in_word in mydict.keys()):

            meta_data =mydict[in_word]

            print("Company     :", meta_data[0])
            print("Release date:", meta_data[1])
            print("Country     :", meta_data[2])
            print("Total screen:", meta_data[3])
            print("Profit      :", meta_data[4])
            print("Total num   :", meta_data[5])
            print("Grade       :", meta_data[6])

        else:
            print("This movie is not in the database.")

But if you carry it out, Enter movie name (End:0) Montage

Company     : (
Release date: Note
Country     : )
Total screen: neck
Profit : s
Total num : t
Grade: N
Enter movie name (exit:0)0

It pops up like this.

I know it was saved incorrectly in mydict, but I don't know how to code it.

python dictionary split

2022-09-20 10:18

1 Answers

for line in fp.readlines():
    line = line.strip()
    x= line.split("|")
    mydict[x[0]] =x[1]
    mydict[x[1]] =x[2]
    mydict[x[2]] =x[3]
    mydict[x[3]] =x[4]
    mydict[x[4]] =x[5]
    mydict[x[5]] =x[6]
    mydict[x[6]] =x[7]
    mydict[x[7]] =x[8]

This is...

for line in fp.readlines():
    line = line.strip()
    x= line.split("|")
    mydict[x[0]] =x[1:]

I'll have to fix it like this.

My preferred method is to read the Pandas' read_csv function with the sep="|" factor.


2022-09-20 10:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.