Re: Extract lines from file, add to new files

Liste des GroupesRevenir à cl python 
Sujet : Re: Extract lines from file, add to new files
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.lang.python
Date : 11. Jan 2024, 19:41:52
Autres entêtes
Organisation : Stefan Ram
Message-ID : <split-20240111194050@ram.dialup.fu-berlin.de>
References : 1
Rich Shepard <rshepard@appl-ecosys.com> writes:
separate names and email addresses

  (Warning: execution of the following script will overwrite
  [DELETE!] files opened with 'w'!)

# create source
data = '''Calvin\ncalvin@example.com\nHobbs\nhobbs@some.com
Nancy\nnancy@herown.com\nSluggo\nsluggo@another.com'''
fn = "data20240111192219+0100"
with open( fn, "w" )as sink: print( data, file=sink )

# split source into two files
with open( fn )as source: file = source.read()
lines = file.split( '\n' )[ :-1 ]
with open( 'salutation.txt', 'w' )as f: print( lines[ 0::2 ], file=f )
with open( 'emails.txt', 'w' )as f: print( lines[ 1::2 ], file=f )

# show the two result files
with open( 'salutation.txt' )as file: print( file.read() )
with open( 'emails.txt' )as file: print( file.read() )


Date Sujet#  Auteur
11 Jan 24 o Re: Extract lines from file, add to new files1Stefan Ram

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal