python作业,急求,在线等,感谢各位大神

2024-12-17 10:07:14
推荐回答(2个)
回答1:

fn1="input.txt"
fn2="output.txt" #"output.html"
fp=open(fn1,"rt")
output_data=[]

while True:
line1=fp.readline()
if line1=="":break
line2=fp.readline()
line3=fp.readline()
output_data.append((line2,line1,line3))

HTML=2
TXT=1
outputtype=HTML
if fn2.endswith("html"):
outputtype=HTML
else:
outputtype=TXT
if outputtype==HTML:
begins="My graphic library

\r\n"
ends="
File NameDate CreatedNumber of Bytes
"
rows="%s%s%s\r\n"

else:
begins=" My GraphicsLibrary\r\n"+\
"File Name\tDate Created\tNumber of Bytes\r\n"
ends=""
rows="%s\t%s\t%s\r\n"

outputs=[begin]
for fname,datec,bytesn in output_data:
outputs.append(rows%(fname,datec,bytesn))
outputs.append(ends)

open(fn2,"wb").write("".join(outputs))

建议你还自己做这道题。这个对文件操作是一个锻练。

回答2:

webfilename = "output.html"
import time
fileName=raw_input('Enter the name of file you want to process: ')
f=open(fileName,'r')
main_heading = '\n'+'%35s'%('My Graphics Library')+'\n'
column_heading = '\n'+'%-19s%0s%20s'%('File Name','Date Created','Number of Bytes')+'\n'
print '\n'+'%35s'%('My Graphics Library')+'\n'
print '\n'+'%-19s%0s%20s'%('File Name','Date Created','Number of Bytes')+'\n'
output_data=[]
date=f.readline().strip()
sumTotal=0
while date!='':
currentYear=time.strftime('%Y',time.strptime(date,'%m/%d/%Y'))
previousYear=currentYear
total=0
sumTotal+=total
while currentYear==previousYear:
name=f.readline().strip()
size=float(f.readline().strip())
print '%-20s%0s%17s' % (name,date,size)

date=f.readline().strip()
total+=size
if date!='':
currentYear=time.strftime('%Y',time.strptime(date,'%m/%d/%Y'))
else:
break
output_data.append((name,date,size))
sumTotal+=total
print '\n'+'%-20s%9s' %('Total Bytes for Files Creadted in '+previousYear,total)+'\n'
print '%-20s%22s' %('Total Bytes for All Files',sumTotal)

choice = raw_input('Do you want open it in web(yes,no): ')
if choice == "yes":

begins = "My graphic library

\r\n"
ends = "
File NameDate CreatedNumber of Bytes
"
rows = "%s%s%s\r\n"

outputs = [begins]
for fname,datec,bytesn in output_data:
outputs.append(rows%(name,date,size))
outputs.append(ends)

open(webfilename,"wb").write("".join(outputs))