Sep-13-2021, 04:38 PM
i'll try to do this in steps:
1- problem: i must rewrite an corrupted m3u file its lines go:
the first part is easy i get a file with plenty of lines that have to modified.
2- tentative solution
3- (new) problem:
that will not append the ".mp3" string at the end of the second (n+1) line, ie :
4.1 trying to "add" the string, of course won't do (that would have been too easy ==> no fun)
4.2 "moving" the cursor to the beginning of the n+1 line
4.3 join i think i have something here. it doesn't work but if you could help me understand why it would be even more interesting, I suspect... of course i wouldn't turn over the answer :)
OS: ubuntu 18.04
python 2.7 but can use python 3.7
1- problem: i must rewrite an corrupted m3u file its lines go:
Output:#EXTINF123#, Title - Song # let's say for clarity sake that the n line
d:\music\title - song.mp3 # this would be the n+1 line
where in fact it should read (caption is important, mind you)Output:#EXTINF123#, Title - Song
Title - Song.mp3
first part is to erase the line that include d:\music... and then this gives a line that has to be rewritten just below itself, becoming the nth+1 linethe first part is easy i get a file with plenty of lines that have to modified.
Output:#EXTINF123#, Title - Song
#EXTINF123#, Title1 - Song1...
so i shift to the main. question2- tentative solution
import in_place with in_place.InPlace('Soothing.m3u') as file: for line in file: file.write(line) file.write(line[12:])
3- (new) problem:
that will not append the ".mp3" string at the end of the second (n+1) line, ie :
Output:#ext123#, Title - Song
Title - Song
4- (new)tentative solutions4.1 trying to "add" the string, of course won't do (that would have been too easy ==> no fun)
file.write(line[12:] + ".mp3")this will add the extension at the beginning of the next line, of course
4.2 "moving" the cursor to the beginning of the n+1 line
with in_place.InPlace('Soothing.m3u') as file: for line in file: file.write(line) file.write(".mp3") file.seek(-5,1) file.write(line[10:])this returns an error
Error:Traceback (most recent call last):
File "erase.py", line 7, in <module>
file.seek(-5,1)
AttributeError: 'InPlace' object has no attribute 'seek'
before, you ask: trying to switch to a .txt file won't change anything to the problem. feel free to test it with a text file, of course.4.3 join i think i have something here. it doesn't work but if you could help me understand why it would be even more interesting, I suspect... of course i wouldn't turn over the answer :)
import in_place with in_place.InPlace('Soothing.m3u') as file: for line in file: file.write(line) a = line[12:] b = ".mp3" c = a.join(b) file.write(c)it just dispatches b all over the place, giving something like:
Output:#EXTINF:135,Title - Song
.Title - Song
mTitle - Song
pTitle - Song
3#EXTINF:194,Title1 - Song1
any help's welcome.OS: ubuntu 18.04
python 2.7 but can use python 3.7