格式信函编程
1.格式信函模板
Welcome back, $1!
We hope that you and all the mambers
of the $0 family are constrantly
reminding your neighbors there
on $5 to shop with up.
As usual, we will ship your order to
$3 $1 $2. $0
$4 $5
$6, $7 $8
...
Public'|'Jane'|'Q','Ms.'|600|'Maple Street'|'Your Town'|'Iowa'|12345
2.格式信函发生器
python
def formLetterSchema(fileName):
field=['Public','Jane','Q','Ms.',600,'Maple Street','Your Town','Iowa',12345]
file = open(fileName)
for line in file:
for index in range(len(field)):
i = index
s='$'+str(i)
# field可能有数字
line = line.replace(s,str(field[index]))
print(line,end='')
if __name__=="__main__":
formLetterSchema("1.txt")

做的练习:
#File:readline
def readLineByOpen(fileName):
file = open(fileName)
while 1:
line = file.readline()
if not line:
break;
print(line,end='')# 输出不换行
#File model fileinput
def readLineByModel_fileinput(fileName):
import fileinput
for line in fileinput.input(fileName):
print(line,end='')# 输出不换行
#File read file by Cache
def readLineByCache(fileName):
file = open(fileName)
for line in file:
print(line,end='')
#File read file by Cache
def readLineByCacheByChar(fileName):
file = open(fileName)
for line in file:
for ch in line:
if ch == '$':
print('find a {$}',end='')
# range
def rangeTest(n):
for i in range(1,n):
print(i,end=' ')
# list
def listTest():
list1=['chen','xinwei',1992,'03',13]
for index in range(len(list1)):
print(index,list1[index])
def formLetterSchema(fileName):
field=['Public','Jane','Q','Ms.',600,'Maple Street','Your Town','Iowa',12345]
file = open(fileName)
for line in file:
for index in range(len(field)):
i = index
s='$'+str(i)
# field可能有数字
line = line.replace(s,str(field[index]))
print(line,end='')
def intToStr(n):
s=str(n)
print(s,end='')
if __name__=="__main__":
#readLineByOpen("1.txt")
#readLineByModel_fileinput("1.txt")
#readLineByCache("1.txt")
#rangeTest(100)
#listTest()
#readLineByCacheByChar("1.txt")
#intToStr(199)
formLetterSchema("1.txt")
网友评论