Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail From: ram@zedat.fu-berlin.de (Stefan Ram) Newsgroups: comp.lang.python Subject: Re: Extract lines from file, add to new files Date: 11 Jan 2024 18:41:52 GMT Organization: Stefan Ram Lines: 22 Expires: 1 Dec 2024 11:59:58 GMT Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de pg/lkq91XrPMFbrtDJoIkwlBDgjZTefJZGQcnUBK3b1YW7 Cancel-Lock: sha1:wR92z3+alJ6G/ziteCdyxbeYrfU= sha256:yUOMqhYB8FVtEpsv1hGsmu7EQKX7l3bbEBTQYNkjTvI= X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved. Distribution through any means other than regular usenet channels is forbidden. It is forbidden to publish this article in the Web, to change URIs of this article into links, and to transfer the body without this notice, but quotations of parts in other Usenet posts are allowed. X-No-Archive: Yes Archive: no X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some services to mirror the article in the web. But the article may be kept on a Usenet archive server with only NNTP access. X-No-Html: yes Content-Language: en-US Accept-Language: de-DE-1901, en-US, it, fr-FR Rich Shepard 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() )