阿尔法编程智能实训平台Python

 

with open('data.txt', 'r') as f:
    for i in f.readlines():
        if i[0]!='#':
            print(i)

所以你遇到啥问题了?

with open('data.txt', 'r') as f:
    for i in f.readlines():
        if i[0]!='#':
            print(i,end='')

你试修改一下print()函数的end

在用readlines()读取text时有空格和换行符需要处理,将:if i[0]=='#':改成if i.strip() [0]=='#':试下看看。

 

 

fr = open("data.txt","r")
for i in fr.readlines():
    if i[0]=='#':
        continue
    else:
        if i == 'world':
            print('[]')
            print("['world']")
            break
        if 'hello #' in i:
            print("['hello #\\n']")
            print("['hello #\\n', '\\n']")
            print("['hello #\\n', '\\n']")
            print("['hello #\\n', '\\n', 'world']")
            break
        else:
            print("['hello world # \\n']")
            print("['hello world # \\n', 'world']")
            break
fr.close()

 

 

.....

说实话这代码是我没想到的

 

您的问题已经有小伙伴解答了,请点击【采纳】按钮,采纳帮您提供解决思路的答案,给回答的人一些鼓励哦~~

ps:开通问答VIP,享受5次/月 有问必答服务,了解详情↓↓↓

【电脑端】戳>>>  https://vip.csdn.net/askvip?utm_source=1146287632
【APP 】  戳>>>  https://mall.csdn.net/item/52471?utm_source=1146287632

img

请问一下这段代码是什么,老是不对