Appending prefix to string in a file
Well i have a sort of telephone directory in a .txt file, what i want to
do is find all the numbers with this pattern e.g. 829-2234 and append the
number 5 to the beginning of the numbers.
so the result now becomes 5829-2234.
my code begins like this:
import os
import re
count=0
#setup our regex
regex=re.compile("\d{3}-\d{4}\s"}
#open file for scanning
f= open("samplex.txt")
#begin find numbers matching pattern
for line in f:
pattern=regex.findall(line)
#isolate results
for word in pattern:
print word
count=count+1 #calculate number of occurences of 7-digit numbers
# replace 7-digit numbers with 8-digit numbers
word= '%dword' %5
well i don't really know how to append the prefix 5 and then overwrite the
7-digit number with 7-digit number with 5 prefix. I tried a few things but
all failed :/
Any tip/help would be greatly appreciated :)
Thanks
No comments:
Post a Comment