解决如下:
1.1
代码:
def str_reverse(s): return s[::-1] if __name__ == '__main__': print(str_reverse("吴彦祖")) def substr(s,x,y): return s[x: x+y] if __name__ == '__main__': print(substr("吴彦祖是帅哥",0,4))
1.2
代码:
def print_file_info(file_name): f = None try: f = open(file_name, "r", encoding="UTF-8") except Exception as e: print(f"捕捉异常!问题是:{e}") else: print(f.read()) finally: if f: #上面已经写了f = None,只要f有内容,那么判别就为True f.close() if __name__ == '__main__': print_file_info("D:/123.txt") def append_to_file(file_name,data): f = open(file_name, "a", encoding="UTF-8") f.write("\n") #换行符也相当于字符串,需要用双引号"" f.write(data) f.close() if __name__ == '__main__': append_to_file("D:/123.txt","帅帅")
1.3
代码:
from my_utils import str_util from my_utils import file_util print(str_util.str_reverse("白马程序员")) print(str_util.substr("白马程序员",0,4)) file_util.print_file_info("D:/123.txt") file_util.append_to_file("D:/123.txt","白马程序员") 运行结果:
我的"D:/123.txt"文本内容:
暗示网易违规与暴雪成立合资公司。
Allusive Netease violates compasses and blizzard to establish joint-stock company.
暗示网易违规与暴雪成立合资公司.
Allusive Netease violates compasses and blizzard to establish joint - stock company.
在中部广阔的土地上,丁磊相信有网易未开垦的处女地。
On mid and vast land, ding Lei believes to have the virgin land with incult Netease.
关于同一事件的另一篇新闻在门户网站网易的点击量排到了第一位.
Another account ranked as the most - viewed on another big portal, Netease.
但网易在企业邮箱市场的立足与发展仍然不可预期。
But Netease still cannot anticipate in the base oneself upon of enterprise mailbox market and development.
可是就是这个愿望也不知道能不能实现,为了网易他身不由己。
But be this desire, also do not know to be able to come true, for Netease his cannot help doing sth.
白马程序员
白马程序员