Replacement expresion - Increment a found number

General questions about using TextPad

Moderators: AmigoJack, bbadmin, helios, MudGuard

Post Reply
jasonbb
Posts: 1
Joined: Mon Mar 15, 2004 4:31 pm

Replacement expresion - Increment a found number

Post by jasonbb »

I have a database that I need to reformat and incrament one of the fields by 1. the data looks like:

4,22,208,,1,2/6/2004 8:53:22,6,,,,0
7,23,165,,1,2/6/2004 8:53:22,5,,,,0
23,1,209,,1,2/6/2004 8:53:22,5,,,,0

It is the 3rd field that I need to increment so the above would change to:

4,22,209,,1,2/6/2004 8:53:22,6,,,,0
7,23,166,,1,2/6/2004 8:53:22,5,,,,0
23,1,210,,1,2/6/2004 8:53:22,5,,,,0

Is it possible to do this with a regular expresion?

Thank You,
Jason
User avatar
MudGuard
Posts: 1295
Joined: Sun Mar 02, 2003 10:15 pm
Location: Munich, Germany
Contact:

Post by MudGuard »

No, regular expressions are not able to do arithmetics.
User avatar
talleyrand
Posts: 624
Joined: Mon Jul 21, 2003 6:56 pm
Location: Kansas City, MO, USA
Contact:

Post by talleyrand »

Install Python and you're good to go.

Copy this code and paste into Textpad.
Save it to something like C:\jasonbb.py
Update the fileName and whether there is a header line

C:\>python jasonbb.py

You can also run it straight out of Texpad. Set the command to wherever you installed Python (probably C:\python23\python.exe)

Code: Select all

import csv

def foo(fileName):

    fin = open(fileName, 'r')
    fout = open(fileName + '.out', 'w')

    r = csv.reader(fin)
    w = csv.writer(fout)
    #Uncomment the following line (remove the #) if there is a header row
#    w.writerow(r.next()) #[edit]Had wrong syntax here[/url]

    for line in r:
        line[2] = str(int(line[2]) + 1)
        w.writerow(line)

def main():
    fileName = r'.\test.csv'
    foo(fileName)

if __name__ == '__main__':
    main()
I choose to fight with a sack of angry cats.
Post Reply