大家好,又见面了,我是你们的朋友全栈君。
文件:
代码:
try:
file=open('food.txt',"r") #以读模式打开文件
except FileNotFoundError: #如果文件不存在,给提示
print("file is not found")
else:
contents=file.readlines() #读取全部行
for content in contents: #显示一行
print(content.split(',')[0]) #每行用逗号分隔后,取第一个元素
运行结果:
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/139336.html原文链接:https://javaforall.cn