regular expressions :-S
please bare with me as im a returning newbie and have not really used re.expression much and still get very confused!
my problem!
i am currently working on a few database records which need appending.
this is one of the files i need to resort
the data base has now changed and i need to strip the first two sections and replace with lower case ie..
S100007|S100007|
would now be
s100007|s100007|
this is the text file i have
stu_file.txt ==
S100007|S100007|Kimf|Shovctter|K.Shogdftter@UCS.AC.UK|Y|Enabled|Y|
S100038|S100038|Lxois|Macvnsfield|L.Manfdfsfield@UCS.AC.UK|Y|Enabled
S100040|S100040|Revxbecca|Harcvxcris|R.Hargrfgis@UCS.AC.UK|Y|Enabled|Y
S100076|S100076|Hocvlly|Wilkxcvins|H.Wifgkins@UCS.AC.UK|Y|Enabled|Y
and so on for about another 3000 odd lines
i can search though the whole file and remove all upper case text but i need the upper case on everything apart from the first to sections but as it only has two capitals and the rest are numbers im stuck on which way i should do it.
import sys
import string
import pprint
import re
newlist=()
inFile = open('Person_Staff.txt','r')
s = inFile.read()
oldStr = ("E.......")
newStr = ("e.......")
inFile.close()
oldlist = re.compile('oldStr').match('inFile', 1)
newlist=(str.replace(oldStr, newStr, oldlist))
outFile = open('Person_Staff.txt','w')
outFile.write(newlist)
outFile.close()
print(newlist)
this is what i have got so far but this just returns errors and i am not shore what to do next or what i haven't done yet!
any help and suggestions would be very helpful!
thanks again!
danholding
Junior Poster in Training
56 posts since Aug 2010
Reputation Points: 15
Solved Threads: 1
Something like this with regular Python:
stu_file = """
S100007|S100007|Kimf|Shovctter|K.Shogdftter@UCS.AC.UK|Y|Enabled|Y|
S100038|S100038|Lxois|Macvnsfield|L.Manfdfsfield@UCS.AC.UK|Y|Enabled
S100040|S100040|Revxbecca|Harcvxcris|R.Hargrfgis@UCS.AC.UK|Y|Enabled|Y
S100076|S100076|Hocvlly|Wilkxcvins|H.Wifgkins@UCS.AC.UK|Y|Enabled|Y"""
for line in stu_file.splitlines():
print line[:10].lower()+line[10:]
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
yes that worked perfectly :)
thank you very much for your help.
for any one else with similar problems
this is the code i ended up with:
# created in python 3.2 by Dan Holding on the "14/03/2011"
import sys, string, pprint, re, os
if len(sys.argv) != 2:
print('Usage: StringSubstitute inputFilename')
print('Where inputFilename is the name of the file to be processed,')
print('I will then remove capital letters from the staff or student number')
print('on each number')
exit(3)
Eline=list()
inFile = open(sys.argv[1],'r')
recordList = inFile.read()
inFile.close()
for line in recordList.splitlines():
Eline = (line[:10].lower()+line[10:]+ '\n')
print(Eline)
outFile1 = open('temp.txt','a')
outFile1.writelines(Eline)
outFile1.close()
inFile1 = open('temp.txt','r')
recordList1 = inFile1.read()
inFile1.close()
outFile = open(sys.argv[1],'w')
outFile.writelines(recordList1)
outFile.close()
os.remove('temp.txt')
danholding
Junior Poster in Training
56 posts since Aug 2010
Reputation Points: 15
Solved Threads: 1
I quess it does do the job, and it is OK to apply 'Don't fix if it is not broken!', but maybe you want to look this:
# created in python 3.2 by Dan Holding on the "14/03/2011"
from __future__ import print_function
import sys, os
if len(sys.argv) != 2:
print('Usage: %s inputFilename' % os.path.split(sys.argv[0])[-1])
print('Where inputFilename is the name of the file to be processed,')
print('I will then remove capital letters from the staff or student number')
print('on each number')
exit(3)
fixed = []
with open(sys.argv[1],'r') as in_file:
for line in in_file:
fixed.append(line[:10].lower()+line[10:])
print(fixed[-1],end='')
with open(sys.argv[1],'w') as out_file:
out_file.write(''.join(fixed))
pyTony
pyMod
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852