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
#8
hello,
thanks to both of you
would you care to explain the code?

    if line.startswith("#EXTINF"): #this makes the loop target a certain line
        out_text +=  line #i see this increments a variable previously set to ""
        name = line.split(",", 1)[1] #i gather this splits line into some sort of array but i fail to grasp its use. 
#could you point me to somewhere this method is explained?
        out_text += f'\n{name}.mp3\n' #at a loss. what's f?
    else:
        out_text += f"{line}\n"
just for the sake of precision, i think it could be good practice to close a file, i guess that print is just to check the whole thing and can be left along, it gives:

in_text = open("playlist.m3u", "r").read()
 
out_text = ""
 
for line in in_text.splitlines():
    if line.startswith("#EXTINF"):
        out_text +=  line
        name = line.split(",", 1)[1]
        out_text += f'\n{name}.mp3\n'
    else:
        out_text += f"{line}\n"
         
with open("new_playlist.m3u", "w") as f:
    f.write(out_text)
f.close()
is it no so?
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 1,102 Jun-19-2023, 08:09 AM
Last Post: Pedroski55
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,591 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,388 Sep-27-2022, 01:38 PM
Last Post: buran
  Inserting line feeds and comments into a beautifulsoup string arbiel 1 1,193 Jul-20-2022, 09:05 AM
Last Post: arbiel
  How to capture string from a line to certain line jerald 1 1,921 Jun-30-2021, 05:13 PM
Last Post: Larz60+
  How to create new line '/n' at each delimiter in a string? MikeAW2010 3 2,854 Dec-15-2020, 05:21 PM
Last Post: snippsat
  How to rename a CSV file by adding MODIFIED in the filename? Python_User 25 8,171 Dec-13-2020, 12:35 PM
Last Post: Larz60+
  How to print string multiple times on new line ace19887 7 5,781 Sep-30-2020, 02:53 PM
Last Post: buran
  Add new line after finding last string in a region Nigel11 1 1,891 Aug-08-2020, 10:00 PM
Last Post: Larz60+
  Searching string in file and save next line dani8586 2 2,317 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