continued from this thread!
http://www.daniweb.com/software-development/python/threads/353210
i have a file which i am currently working on which will remove all pipes and replace them with a comma. that all works fine it the second bit i am currently stuck on..
i need to remove the first two CAPITAL letters from the numbers ie.

WHAT I NEED \/
EXTERNAL_PERSON_KECVY|USER_ID|FIRSTNAME|LASTNAME|EMAIL|EMAIL_IND
s100007|s100007|KiCVBm|ShotGDFGDter|K.ShoBBCVBtter@UCS.AC.UK|Y|Enabled|Y|
s100038|s100038|Lois|MansVBCVBfield|L.MansFGDFGfield@UCS.AC.UK|Y|Enabled|

WHAT I GET! \/
external_pERSON_KECVY|USER_ID|FIRSTNAME|LASTNAME|EMAIL|EMAIL_IND
s100007|s100007|KiCVBm|ShotGDFGDter|K.ShoBBCVBtter@UCS.AC.UK|Y|Enabled|Y|
s100038|s100038|Lois|MansVBCVBfield|L.MansFGDFGfield@UCS.AC.UK|Y|Enabled|

AS U CAN SEE THE FIRST LINE IS ALSO IN CAPS BUT I NEED TO KEEP IT THAT WAY.

HOW CAN I SORT THOUGH THE FILE AND REMOVE THE CAPS APART FROM THE FIRST LINE!

THIS IS WHAT I HAVE SO FAR

# 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: CapR_PipeR inputFilename')
    print('Where inputFilename is the name of the file to be processed,')
    print('It will then remove capital letters from the staff or student number')
    print('and replace pipes(|) with commas(,)on each line ')
    exit(3)
Eline=list()
inFile = open(sys.argv[1],'r')
recordList = inFile.read()
inFile.close()
oldChar = '|'
newChar = ','
newRecordList = []
pprint.pprint( recordList)
pprint .pprint(sys.argv)
if re.match('.+', oldChar):
    for record in recordList:
        newRecordList.append(str.replace(record,oldChar,newChar))
outFile = open(sys.argv[1],'w')
outFile.writelines(newRecordList)
outFile.close()


inFile = open(sys.argv[1],'r')
recordList = inFile.read()
inFile.close()
lineN = 0
for line in recordList.splitlines():
    lineN +=1
    if lineN ==0:print(lineN)
    else:
        lineN +=1
        Eline = (line[:10].lower()+line[10:]+ '\n')
    
    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)
os.remove('temp.txt')

Recommended Answers

All 2 Replies

Slight change from the code I already posted to you:

from __future__ import print_function
# created in python 3.2 by Dan Holding and Tony Veijalainen on the "14/03/2011"
import sys, os
if len(sys.argv) != 2:
    print('''Usage: %s inputfilename
Where inputFilename is the name of the file to be processed,
I will then remove capital letters from the staff or student number
on each number passing the first line''' %  os.path.split(sys.argv[0])[-1])
    exit(3)

with open(sys.argv[1],'r') as in_file:
    fixed=  [next(in_file)]
    print(fixed[-1], end='')
    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))
commented: fantastic work! :) +1

thank you soo much for your help!
i have now got it working perfectly :) :) :)
ME == very happy

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.