-2

i have been trying to open a file and make some edits in it using regular Expressions. The code seems fine but i still get an error saying io.UnsupportedOperation: not readable

Code is as follows:

import re

def fun(o_date):
    print(o_date)
    l = o_date.split('-')
    r=[]
    r[0]='20'+l[0]
    if l[1]=='Jan':
        r[1]='01'
    elif l[1]=='Feb':
        r[1]='02'
    elif l[1]=='Mar':
        r[1]='03'
    elif l[1]=='Apr':
        r[1]='04'
    elif l[1]=='May':
        r[1]='05'
    elif l[1]=='Jun':
        r[1]='06'
    elif l[1]=='Jul':
        r[1]='07'
    elif l[1]=='Aug':
        r[1]='08'
    elif l[1]=='Sep':
        r[1]='09'
    elif l[1]=='Oct':
        r[1]='10'
    elif l[1]=='Nov':
        r[1]='11'
    elif l[1]=='Dec':
        r[1]='12'
    r[2]=l[0]
    r1 = str(r[0])+'-'+str(r[1])+'-'+str(r[2])
    return r1

if __name__ == "__main__":
    print("hello 0.1")
    f = open('Finance.sql','w')
    print("hello 1.1")
    r = f.read()
    print("hello1")
    s = re.compile(r'\d{2,2}-[a-zA-Z]{3-3}-\d{2,2}')
    for i in r:
        print("hello : ", i)
        re.sub(s,fun,i)

The Finance.sql files just contains insert commands into the table.

Any help is appreciated. Thanks

nik
  • 576
  • 2
  • 7
  • 14

1 Answers1

1

The following line says you want to only write to your file (note the 'w'):

f = open('Finance.sql','w')

Opening a file for read and write (non truncated, python3):

f = open('Finance.sql','r+')

You should use with to cleanly open and close the file (see e.g. this post)

Are you sure you want to open a .sql file in this way?

Community
  • 1
  • 1
syntonym
  • 7,134
  • 2
  • 32
  • 45