Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The evenements.txt has a total of 160 lines. I wont go into too many details as i want to be able to learn the rest of the code by myself. But when i run this code using the for loop in liste_evenement , i believe it only returns the last line of the evenements.txt instead of all lines. What am i missing? The code is supossed to return a list of events separated in different lists. So there are lists in my list and im using the .split to separate the element in my lists(this is related to my evaluation but this isnt about my question, i am just giving you guys some background). But my actual question here is about the problem when i print my words which is not returning the whole file.




with open("evenements.txt", "r", encoding="utf-8-sig") as evenement:
    liste_evenement = evenement.readlines()

    words = []
    for line in liste_evenement:
        words = line.split(" ")
    
    print(words)




Code has to return :


[['Danse', 'Samedi de danser...', 202, 202],
                      ['Musique', 'Peter Gunn : spécial rétro francophone', 203, 205],
                      ['Musique', 'Eletric Power Trio', 212, 212],
                      ['Théâtre', 'Hamlet', 212, 212]]



its french, but you get the picture. so every lists of my global list is an event. And so far its only returning :

['Musique/Festival', 'OFF', 'de', 'Québec/2018-07-04/2018-07-08']


I believe this is the last line of file that has a total of 160 lines as mentionned. Now if you could also help me remove those '/' in the code, thatd be great. Because im having a hard time manipulating list. Thank you !




EDIT : I managed to put them all in different lists but im having trouble adding them all to another list and im also having trouble removing the '\n'

Updated code :

with open("evenements.txt", "r", encoding="utf-8-sig") as evenement:
    liste_evenement = evenement.readlines()

    words = []

    for line in liste_evenement:
        words = line.split("/")

        print(words)


Heres what its returning right now :

['Musique', 'Shawn Phillips', '2018-08-24', '2018-08-24\n']
['Musique', "L'avenue Royale fête l'été!", '2018-08-25', '2018-08-25\n']'['Musique', 'Perséides musicales', '2018-08-03', '2018-08-03\n']
['Musique', 'Gaétan Leclerc chante Félix et…', '2018-08-17', '2018-08-17\n']
['Musique', 'The Ring of Fire : a Johnny Cash Experience', '2018-07-21', '2018-07-21\n']
['Musique', "Jazz'Art", '2018-07-27', '2018-07-29\n']
['Musique', 'Génération Crooners', '2018-07-07', '2018-07-07\n']
['Musique', 'Fiesta Latina avec Me llamo Son', '2018-07-14', '2018-07-14\n']
['Musique', "L'avenue Royale fête l'été!", '2018-08-25', '2018-08-25\n']
['Musique', 'Concert au jardin', '2018-07-04', '2018-07-04\n']
['Musique', 'Concert au jardin', '2018-07-11', '2018-07-11\n']



And i want it to return :



[['Musique', 'Shawn Phillips', '2018-08-24', '2018-08-24]
['Musique', "L'avenue Royale fête l'été!", '2018-08-25', '2018-08-25]
['Musique', 'Perséides musicales', '2018-08-03', '2018-08-03]
['Musique', 'Gaétan Leclerc chante Félix et…', '2018-08-17', '2018-08-17]'['Musique', 'The Ring of Fire : a Johnny Cash Experience', '2018-07-21', '2018-07-21]
['Musique', "Jazz'Art", '2018-07-27', '2018-07-29]
['Musique', 'Génération Crooners', '2018-07-07', '2018-07-07]'['Musique', 'Fiesta Latina avec Me llamo Son', '2018-07-14', '2018-07-14]
['Musique', "L'avenue Royale fête l'été!", '2018-08-25', '2018-08-25]
['Musique', 'Concert au jardin', '2018-07-04', '2018-07-04]'['Musique', 'Concert au jardin', '2018-07-11', '2018-07-11]]


So im having trouble doing a list of those lists and removing the '\n' in my lists.

What I have tried:

I tried so many things that i cant even remember. This is the only code that allowed me to remove the backslash while being able to seperate my list with a " " using the split function. I had a hard time using .split() and a bunch of other stuff on a list.
Posted
Updated 10-Oct-18 7:55am
v3
Comments
Mohibur Rashid 10-Oct-18 1:35am    
Look for two dimentional array
Richard MacCutchan 10-Oct-18 3:19am    
If it was you who voted a 1 to all those answers, you need to read through them again and ask yourself what is wrong with them.
Patrice T 10-Oct-18 13:48pm    
you should add updated code.

Well yes - it will.
Each time you go round the loop, you overwrite the content of words - so teh only text you see after the loop is the last one you added.

You need to use the append method: Python Arrays[^]
 
Share this answer
 
Quote:
What am i missing? The code is suposed to return a list of events separated in different lists.

Python
with open("evenements.txt", "r", encoding="utf-8-sig") as evenement:
    liste_evenement = evenement.readlines()

    words = []
    for line in liste_evenement:
        words = line.split(" ") # replace variable words with the words of current line
                                # but previous contain is lost
    
    print(words) # this prints list of words, but only after the loop

Python
with open("evenements.txt", "r", encoding="utf-8-sig") as evenement:
    liste_evenement = evenement.readlines()

    words = []
    for line in liste_evenement:
        words = line.split(" ")
        print(words) # this prints list of words for each line

To get the result you want, you need to append the result of line.split(" ") in words.
see documentation for details.
 
Share this answer
 
Comments
Member 13870077 10-Oct-18 11:55am    
Thank you but i just noticed that i still had the backslash ('\n') from my file and its pretty hard to use .strip on a list is it ? Is there a way i could remove it while keeping this code
Patrice T 10-Oct-18 12:07pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Member 13870077 10-Oct-18 15:57pm    
done.
On the first look, you have to split lines on multiple delimiters:

  • [
  • ]
  • ,


[EDIT]
Seems you need a second loop too - to be able to print single "word".

See:
How to use Split in Python[^]
Python: Split string with multiple delimiters - Stack Overflow[^]
ForLoop - Python Wiki[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900