Java教程

py学习记录#11

本文主要是介绍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”的形式输入文件名,其示例内容如下所示)

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文件的示例内容。

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)

 

这篇关于py学习记录#11的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!