Skip to content Skip to sidebar Skip to footer

Open Txt File In Python

i need to open a txt file . In txt file i have Andrei:Popescu:Bucuresti Maria:Popescu:Targu-Mures .... How do I read a text file into three variable and for each line do somethin

Solution 1:

Notice that the names are separated by a colon(:) so add : in split() to split them and store them in multiple variables:

    with open("filename.txt") as f:
        for line in f :
            word1,word2,word3 = line.split(":")

    print(word1)
    print(word2)
    print(word3)

Post a Comment for "Open Txt File In Python"