Home > Blockchain >  How do you append the same text to multiple txt files in python?
How do you append the same text to multiple txt files in python?

Time:08-08

Imagine you have some text that you want to split into chunks and send to separate files which are named using l'mahdi's solution found here

But imagine there is a header at the top of the file:

company:: acme products
department:: sales
floor:: 1

name:: Joe Blogs 
phone:: 123456789
email:: [email protected]
address:: 123 Main Street
note:: blah blah blah
timestamp::

name:: Josephine Blogs 
phone:: 43217890
email:: [email protected]
address:: 123 Main Street
note:: More blah blah
timestamp::

name:: John Smith 
phone:: 23498689
email:: [email protected]
address:: 1 North Street
note:: Some more blah
timestamp::

How do you append the header to each file so we get the following files?

# chunk_1.txt
name:: Joe Blogs 
phone:: 123456789
email:: [email protected]
address:: 123 Main Street
note:: blah blah blah
timestamp:: 2022-07-15 (15h 12m 25s)
company:: acme products
department:: sales
floor:: 1


# chunk_2.txt
name:: Josephine Blogs 
phone:: 43217890
email:: [email protected]
address:: 123 Main Street
note:: More blah blah
timestamp:: 2022-07-15 (15h 12m 26s)
company:: acme products
department:: sales
floor:: 1


# chunk_3.txt
name:: John Smith 
phone:: 23498689
email:: [email protected]
address:: 1 North Street
note:: Some more blah
timestamp:: 2022-07-15 (15h 12m 27s)
company:: acme products
department:: sales
floor:: 1

CodePudding user response:

Just split the header firstly, you can try my solution:

from datetime import datetime
import time
import re

with open('file.txt') as f:
    header, content = f.read().split('\n\n', maxsplit=1)
    for n, chunk in enumerate(content.split('\n\n'), start=1):
        timestamp = datetime.now().strftime('%Y-%m-%d (%Hh %Mm %Ss)')
        chunk = re.sub(r'(timestamp::)', fr'\1 {timestamp}', chunk)
        chunk = chunk.strip()   '\n'   header
        with open(f'chunk_{n}.txt', 'w') as f_out:
            f_out.write(chunk)
        time.sleep(1)

And here is the result:

# chunk_1.txt

name:: Joe Blogs
phone:: 123456789
email:: [email protected]
address:: 123 Main Street
note:: blah blah blah
timestamp:: 2022-08-07 (13h 10m 08s)
company:: acme products
department:: sales
floor:: 1

# chunk_2.txt

name:: Josephine Blogs
phone:: 43217890
email:: [email protected]
address:: 123 Main Street
note:: More blah blah
timestamp:: 2022-08-07 (13h 10m 09s)
company:: acme products
department:: sales
floor:: 1

# chunk_3.txt

name:: John Smith
phone:: 23498689
email:: [email protected]
address:: 1 North Street
note:: Some more blah
timestamp:: 2022-08-07 (13h 10m 10s)
company:: acme products
department:: sales
floor:: 1
  • Related