py学习记录#11

T1

程序:编写Python程序以读取和打印``egypt.txt''文件中的每一行。 下面给出了” egypt.txt”文件的示例内容。

 Printing each line in the text file

Ancient Egypt was an ancient civilization of eastern North Africa, concentrated along

the lower reaches of the Nile River.

fi=open("egypt.txt","r+",encoding='utf-8')
while 1:
    temp=fi.readline()
    if temp=="":
        break
    print(temp)
fi.close()

 

T2

程序:使用with语句读取和打印“ japan.txt”文件中的每一行的程序。 下面给出了“ japan.txt”文件的示例内容。

 National Treasures of Japan are the most precious of Japan's Tangible Cultural Properties.

A Tangible Cultural Property is considered to be of historic or artistic value, classified either as

"buildings and structures", or as "fine arts and crafts".

fi=open("japan.txt","r+",encoding='utf-8')
for i in fi:
    print(i)
fi.close()

 

T3

 程序:编写Python程序以使用read()方法(或者readlines())读取“ rome.txt”文件。 下面给出了“ rome.txt”文件的示例内容。

Ancient Rome was a civilization which began on the Italian Peninsula in the 8th century BC.

The Roman Emperors were monarchial rulers of the Roman State.

The Emperor was supreme ruler of Rome.

Rome remained a republic.

fi=open("rome.txt","r+",encoding='utf-8')
for i in fi:
    print(i)
fi.close()

 

T4

程序:编写Python程序以在文件中查找最长的单词。 从用户那里获取文件名。 (假设用户以“ animals.txt”的形式输入文件名,其示例内容如下所示)

py学习记录#11

fi=open("text.txt","r+",encoding="utf-8")
maxn=""
for i in fi:
    tmp=i.split()
    for j in tmp:
        maxn=max(j,maxn,key=len)
print(maxn)

 

T5

程序:考虑“ Sample_Program.py” Python文件。编写Python程序,以从给定Python源文件中的所有行中删除注释字符。下面给出了“ Sample_Program.py” Python文件的示例内容。

py学习记录#11

fi=open("text.txt","r+",encoding="utf-8")
for i in fi:
    print(i.replace("#",""))

 

T6

程序:编写Python程序来反转“ secret_societies.txt”文件中的每个单词。 下面给出了“ secret_societies.txt”的示例内容。

terceS seiteicoS

snosameerF itanimullI

snaicurcisoR grebredliB sthginK ralpmeT

fi=open("text.txt","r+",encoding="utf-8")
for i in fi:
    tmp=i.split()
    for j in tmp:
        t=list(j)
        t.reverse()
        print(t)

 

上一篇:shell if高级


下一篇:P3266 [JLOI2015]骗我呢