.txt ファイルに一種の電話帳があります。やりたいことは、このパターン (829-2234 など) を持つすべての番号を検索し、番号の先頭に番号 5 を追加することです。
したがって、結果は 5829-2234 になります。
私のコードは次のように始まります:
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
プレフィックス5を追加してから、7桁の数字を5プレフィックスの7桁の数字で上書きする方法がよくわかりません。私はいくつかのことを試しましたが、すべて失敗しました:/
ヒント/ヘルプをいただければ幸いです:)
ありがとう