新人求上岸 发表于 2021-7-20 22:26:33

unexpected character after line continuation character怎么解决

file_name = input("请输入文件名:")
old_word = input("请输入需要替换的单词或字符:")
new_word = input("请输入新的单词或字符:")
file = open(file_name,encoding ='utf-8')
storehouse = []
count = 0
for i in file:
    storehouse.append(i)
for each_line in storehouse:
    if old_word in each_line:
      count +=1
      each_line.replace(old_word,new_word)
print("文件",file_name,"中共有",count,"个【",old_word,"】")
decision = input("你确定要吧所有的【",old_word,"】替换为【",new_word,"】吗?"\n"【YES/NO】:")
if decision in ["Yes",'yes',"YES"]:
    file_write = open(file_name,'w')
    file_write.writelines(storehouse)
    file_write.close()
file.close()
第029讲:文件:一个任务 | 课后测试题及答案
https://fishc.com.cn/thread-45487-1-1.html
(出处: 鱼C论坛)
最后一题,在file_write = open(file_name,'w')这一行运行总会显示unexpected character after line continuation character
怎么办

fish_游鱼 发表于 2021-7-20 22:30:55

decision = input('你确定要吧所有的【",old_word,"】替换为【",new_word,"】吗?\"\n"【YES/NO】:')
最外层应该使用单引号

新人求上岸 发表于 2021-7-20 22:54:02

fish_游鱼 发表于 2021-7-20 22:30
最外层应该使用单引号

谢谢 问题也解决

名字只有七个字 发表于 2021-7-21 07:48:36

新人求上岸 发表于 2021-7-20 22:54
谢谢 问题也解决

另一个方法decision = input("你确定要吧所有的【\",old_word,\"】替换为【\",new_word,\"】吗?\"\n\"【YES/NO】:")
页: [1]
查看完整版本: unexpected character after line continuation character怎么解决