BrownRecluse
BrownRecluse

Reputation: 1670

Python appending after updating the line to a file

I have a text file with content:

pb333u$Paul Barron   $20001:24001:26001:28001$30001:34001:36001:38001$40001:44001:46001:48001::

pk7319$Patrick Val      $20002:24002:26002:28002$30002:34002:36002:38002$40002:44002:46002:48002::

pb348u$Victor Ataler$20003:24003:26003:28003$30003:34003:36003:38003$40003:44003:46003:48003::

What I want is to append another line to the file, such as:

th3674$Tom Harry$20004:24004:26004:28004$30004:34004:36004:38004$40004:44004:46004:48004::

Basically, with a new id and name; and each number incremented by 1.

I wrote this python program to achieve the above:

def parse_file1():
   id_dict = {'at_id':'th3674','full_name':'Tom Harry'}
   f=open('ports.dat','a+')
   lines = f.readlines()
   if lines:
       last = lines[-1]
       last = last.replace(last.split('$')[0],id_dict['at_id'])
       last = last.replace(last.split('$')[1],id_dict['full_name'])
       split_colon(last)
       f.write('\n')
       f.write(last)

def split_colon(line):
    for i in range(2,5): 
        key = line.split('$')[i]
        for j in range(4):
            v = key.split(':')[j]
            line = line.replace(v, str(int(v) + 00001))

if __name__=='__main__':
    parse_file1()

The output I am getting is:

pb333u$Paul Barron   $20001:24001:26001:28001$30001:34001:36001:38001$40001:44001:46001:48001::

pk7319$Patrick Val      $20002:24002:26002:28002$30002:34002:36002:38002$40002:44002:46002:48002::

pb348u$Victor Ataler$20003:24003:26003:28003$30003:34003:36003:38003$40003:44003:46003:48003::

th3674$Tom Harry$20003:24003:26003:28003$30003:34003:36003:38003$40003:44003:46003:48003::

I am not able to increment the numbers in the last line by 1. Also, is there a better way to achieve the above? I feel my program is a bit too un-generic.

Upvotes: 0

Views: 40

Answers (1)

Anand S Kumar
Anand S Kumar

Reputation: 90889

The issue is that strings are immutable , and in your split_colon() function, when you do -

line = line.replace(v, str(int(v) + 00001))

This does not change the actual last string in your parse_file1() function. You need to return the changed line back, and set it back to last variable, before writing it out to file. Example -

def parse_file1():
   id_dict = {'at_id':'th3674','full_name':'Tom Harry'}
   f=open('ports.dat','a+')
   lines = f.readlines()
   if lines:
       last = lines[-1]
       last = last.replace(last.split('$')[0],id_dict['at_id'])
       last = last.replace(last.split('$')[1],id_dict['full_name'])
       last = split_colon(last)
       f.write('\n')
       f.write(last)

def split_colon(line):
    for i in range(2,5): 
        key = line.split('$')[i]
        for j in range(4):
            v = key.split(':')[j]
            line = line.replace(v, str(int(v) + 1))
    return line

if __name__=='__main__':
    parse_file1()

Also, just add 1 to it, no need to define the number as - 00001 (In Python 2.x , when you start a number with 0 , it becomes an octal literal , though that does not affect your program).

Upvotes: 3

Related Questions