Python Forum
append a string to a modified line
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
append a string to a modified line
#1
i'll try to do this in steps:

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 line

the 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. question

2- 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 solutions

4.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
Reply


Messages In This Thread
append a string to a modified line - by Mr_Blue - Sep-13-2021, 04:38 PM
RE: append a string to a modified line - by Mr_Blue - Sep-14-2021, 05:15 PM
RE: append a string to a modified line - by Mr_Blue - Sep-14-2021, 06:10 PM
RE: append a string to a modified line - by Mr_Blue - Sep-15-2021, 05:06 PM
RE: append a string to a modified line - by Mr_Blue - Sep-16-2021, 07:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PDF properties doesn't show created or modified date Pedroski55 4 2,528 Jun-19-2023, 08:09 AM
Last Post: Pedroski55
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 15,439 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 3,683 Sep-27-2022, 01:38 PM
Last Post: buran
  Inserting line feeds and comments into a beautifulsoup string arbiel 1 2,165 Jul-20-2022, 09:05 AM
Last Post: arbiel
  How to capture string from a line to certain line jerald 1 2,481 Jun-30-2021, 05:13 PM
Last Post: Larz60+
  How to create new line '/n' at each delimiter in a string? MikeAW2010 3 6,403 Dec-15-2020, 05:21 PM
Last Post: snippsat
  How to rename a CSV file by adding MODIFIED in the filename? Python_User 25 13,069 Dec-13-2020, 12:35 PM
Last Post: Larz60+
  How to print string multiple times on new line ace19887 7 8,468 Sep-30-2020, 02:53 PM
Last Post: buran
  Add new line after finding last string in a region Nigel11 1 2,444 Aug-08-2020, 10:00 PM
Last Post: Larz60+
  Searching string in file and save next line dani8586 2 2,925 Jul-10-2020, 09:03 AM
Last Post: dani8586

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020