Home > Enterprise >  Using python, how do you add incremental timestamps to .txt file?
Using python, how do you add incremental timestamps to .txt file?

Time:07-15

Suppose I have some text I want to split into chunks that end up in separate text files (and I want to use mozway's solution to that task found here), how do I add incremental timestamps to the text before splitting?

So, imagine the .txt file I want to updated is as follows:

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::

...and I want to add a timestamp to each chunk, separated by 1 second. The result would look like this:

name:: Joe Blogs 
phone:: 123456789
email:: [email protected]
address:: 123 Main Street
note:: blah blah blah
timestamp:: 2022-07-15 (14h 55m 23s)

name:: Josephine Blogs 
phone:: 43217890
email:: [email protected]
address:: 123 Main Street
note:: More blah blah
timestamp:: 2022-07-15 (14h 55m 24s)

name:: John Smith 
phone:: 23498689
email:: [email protected]
address:: 1 North Street
note:: Some more blah
timestamp::  2022-07-15 (14h 55m 25s)

I then want to split this text using the working solution that mozway gave here.

I tried the following code but it didn't work...

import time

with open('file.txt') as f:
    for line in f:
        if line.startswith('timestamp::'):
            t = time.localtime()
            timestamp = time.strftime('%Y-%m-%d (%Hh %Mm %Ss)', t)
            line = line   timestamp
            time.sleep(1)
    for n, chunk in enumerate(f.read().split('\n\n'), start=1):
        with open(f'chunk_{n}.txt', 'w') as f_out:
            f_out.write(chunk)

Only one file was generated, 'chunk_1.txt', and it was empty; it had no content.

CodePudding user response:

You can use datetime.now() and use re.sub from regex to replace timestamp:: to timestamp:: %Y-%m-%d (%Hh %Mm %Ss).

from datetime import datetime
import time
import re

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

Output:

# 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)

# ---------------
# 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)

# ---------------
# 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)

CodePudding user response:

  1. check line feed in your system (\n vs \n\r vs \r\n)
  2. maybe save chunks in 1st loop?
  3. where exactly are you updating the content of 1st file? You cannot just do line = line timestamp

You can add enumeration to 1st loop and save parts (list of lines) there

  • Related