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
#10
(Sep-15-2021, 05:06 PM)Mr_Blue Wrote: i think it could be good practice to close a file
with open("new_playlist.m3u", "w") as f:
    f.write(out_text)
f.close() # Not needed
The point of using with open is that it close the file automatically.
Quote:would you care to explain the code?
Learn to take out pieces and test it interactively on of the strength of Python,
then easier to see what going on,an loop is just doing this serval times 🧬
>>> out_text = ""
>>> out_text += 'Hello'
>>> out_text += 'World'
>>> out_text
'HelloWorld'
>>> out_text = ""
>>> line = '#EXTINF:137,Choir - When Johnny Comes Marching Home'
name = line.split(",", 1)[1]
>>> name
'Choir - When Johnny Comes Marching Home'

>>> out_text += f'\n{name}.mp3\n'
>>> out_text
'\nChoir - When Johnny Comes Marching Home.mp3\n'
Or a other option as in my code that don't collect in out_text,just write it directly to the file.
with open('songs.txt') as f,open('songs_out.txt', 'w') as f_out:
    for line in f:
        line = line.strip()
        if line.startswith("#EXTINF"):
            f_out.write(f'{line}\n')
            name = line.split(",")[1]
            f_out.write(f'{name}.mp3\n')
Output:
#EXTINF:137,Choir - When Johnny Comes Marching Home Choir - When Johnny Comes Marching Home.mp3 #EXTINF:191,Glenn Miller - When Johnny Comes Marching Home Glenn Miller - When Johnny Comes Marching Home.mp3 #EXTINF:308,Chad Mitchell Trio - Johnnie Chad Mitchell Trio - Johnnie.mp3 #EXTINF:205,Joan Baez - Johnny, I Hardly Knew Yeh Joan Baez - Johnny.mp3 #EXTINF:213,Clancy Brothers & Tommy Makem - Johnny I Hardly Knew You Clancy Brothers & Tommy Makem - Johnny I Hardly Knew You.mp3
Mr_Blue likes this post
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 snippsat - Sep-16-2021, 03:35 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,153 Jun-19-2023, 08:09 AM
Last Post: Pedroski55
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,694 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,417 Sep-27-2022, 01:38 PM
Last Post: buran
  Inserting line feeds and comments into a beautifulsoup string arbiel 1 1,221 Jul-20-2022, 09:05 AM
Last Post: arbiel
  How to capture string from a line to certain line jerald 1 1,941 Jun-30-2021, 05:13 PM
Last Post: Larz60+
  How to create new line '/n' at each delimiter in a string? MikeAW2010 3 2,899 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,283 Dec-13-2020, 12:35 PM
Last Post: Larz60+
  How to print string multiple times on new line ace19887 7 5,848 Sep-30-2020, 02:53 PM
Last Post: buran
  Add new line after finding last string in a region Nigel11 1 1,916 Aug-08-2020, 10:00 PM
Last Post: Larz60+
  Searching string in file and save next line dani8586 2 2,336 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