-3

I am trying to put my file.txt into a list and then each index into different lists while all the lists remain in the initial list. I have been able to add it into a single list so far.

Here's my code:

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

I want my code to return :

[['index_1'],['index_2'], ...]

Do I have to use a for loop? or even a while? I'm a little confused because of the \n at the end of every line of my evenements.txt, so if you could help me with that because I am familiar with .strip() when I use it on a string but its not working on a list.

EDIT : here's a sample of what i need to do now :

My code is now returning :

[['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']]

What i need to do now is that i need to add a " for every '/' in my code so a " between Musique and shawn and another one between phillips and 2018, except between the Year/Month/Day (just look at the result down below. Keep in mind that this is just a part of the code and my list actually contains 160 index.

so heres the result that im looking for :

[['Danse', 'Samedi de danser...', 202, 202],
Oxynor
  • 41
  • 6
  • 4
    What is _each index_? You have not mentioned any indexes. – DYZ Oct 09 '18 at 05:32
  • Well, the evenement.readlines() returns a list of my evenements.txt right ? So i want each index of my list to become a list of its own while they remain in the initiat list so it would be something like that : [['index 1'],['index 2']] – Oxynor Oct 09 '18 at 05:34
  • so does the file have a filename/path per file? – Roman A. Taycher Oct 09 '18 at 05:39
  • Possible duplicate of [Strip all the elements of a string list](https://stackoverflow.com/questions/7984169/strip-all-the-elements-of-a-string-list) – AGN Gazer Oct 09 '18 at 05:41
  • Please include a sample of your file and the expected output. Is your file called file.txt or evenements.txt? – DYZ Oct 09 '18 at 05:48
  • @AGNGazer Yes thanks for the link, but the main issue is about the file tho! – Oxynor Oct 09 '18 at 05:48
  • 1
    Your first sentence: _"I am trying to put my `file.txt` into a list and then each index into different lists while all the lists remain in the initial list."_ is absolutely confusing. I can't figure it out. As suggested by @DYZ, an example of data and desired output would greatly help. – AGN Gazer Oct 09 '18 at 05:51
  • 1
    Where are indices in `[['index_1'],['index_2'], ...]` supposed to come from? Are these part of each line? Why there is only one index per innermost list??? – AGN Gazer Oct 09 '18 at 06:11
  • How about providing an example file content and desired output based on _that_ example file. – AGN Gazer Oct 09 '18 at 06:12
  • Can we get a sample of the text you are opening – vash_the_stampede Oct 09 '18 at 18:07

1 Answers1

-1

You can use slicing and a comprehension for that:

with open("yourfile", "r") as f:
    number_of_lists = 4
    lists = [f[i::number_of_lists] for i in range(number_of_lists)]

Not sure if you can slice a file directly; if not make f a list with all lines in it. as has been pointed out in the comments you can't directly but can use itertools.islice to do so. Or make a list

This takes every nth element from the file and offsets the start of this nth with n.

EDIT: Just read your comment, if you want each line to be a list:

with open("file" ,"r") as f:
    list = [[x] for x in f]

EDIT2: to strip newlines:

with open("yourfile", "r") as f:
    number_of_lists = 4
    lists = [[x.rstrip("/n") for x in f[i::number_of_lists]] for i in range(number_of_lists)]
SV-97
  • 431
  • 3
  • 15
  • 1
    You cannot slice a file object directly, but you can use `itertools.islice` to do so. – Adam Smith Oct 09 '18 at 05:46
  • @SV-97 thanks ! i didnt know i could do that. ill keep that in mind ! – Oxynor Oct 09 '18 at 05:55
  • @SV-97 but is this possible to also stack another for loop to remove the '\n' like AGN Gazer suggested – Oxynor Oct 09 '18 at 06:04
  • Added a version that strips newlines – SV-97 Oct 09 '18 at 07:26
  • @SV-97 i always get the error message '_io.TextIOWrapper' object is not subscriptable – Oxynor Oct 09 '18 at 16:51
  • @Oxynor IIRC that happens if you try to slice it directly. Put a `content = [x for line in f]` before the lists Line and change it so that it slices from content instead of f. Or see the crossed out section in my comment – SV-97 Oct 09 '18 at 17:03