鱼C论坛

 找回密码
 立即注册
查看: 1391|回复: 2

[已解决]仿照写的课后作业,还没咋运行,每次都显示最后一行有问题 invalid synatx

[复制链接]
发表于 2019-10-24 18:15:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
是不是我哪里有问题,导致最后一步,无法调用函数,一调用就出错
import os

def pos_in_line (line,key):
    pos= []
    begin = line.find(key,0)
    if begin != -1:
        pos.append (begin+1)
        begin = line.find(key,begin+1)
    return pos
def pos_in_file (file , key):
    f = open (file)
    count = 0
    key_dict= dict()
    for each_line in file:
        count +=1
        key_dict[count] = pos_in_line (each_line ,key)
    f.close()
    return key_dict

def search_files(key,path):
    txt_files=[]
    dict1= dict()
    for root,dirs, files in os.walk(path):
        for name in files:
            if os.path.splitext[1]== '.txt':
                txt_name = os.path.jion(root,name)
                txt_files.append (text_name)
    for file in txt_files:
        dict1 = pos_in_file (file , key)
        num = dict1.key()
        num = sorted(num)
        for each in num:
            print ('在有%s处,关键字位于%s,第%s个位置' % (file, num, dict1[num])


search_files('爱','G:/python/测试/30.4')
最佳答案
2019-10-24 18:49:24
print ('在有%s处,关键字位于%s,第%s个位置' % (file, num, dict1[num]))

少了个括号,补上就行了

  1. import os

  2. def pos_in_line (line,key):
  3.     pos= []
  4.     begin = line.find(key,0)
  5.     if begin != -1:
  6.         pos.append (begin+1)
  7.         begin = line.find(key,begin+1)
  8.     return pos
  9. def pos_in_file (file , key):
  10.     f = open (file)
  11.     count = 0
  12.     key_dict= dict()
  13.     for each_line in file:
  14.         count +=1
  15.         key_dict[count] = pos_in_line (each_line ,key)
  16.     f.close()
  17.     return key_dict

  18. def search_files(key,path):
  19.     txt_files=[]
  20.     dict1= dict()
  21.     for root,dirs, files in os.walk(path):
  22.         for name in files:
  23.             if os.path.splitext[1]== '.txt':
  24.                 txt_name = os.path.jion(root,name)
  25.                 txt_files.append (text_name)
  26.     for file in txt_files:
  27.         dict1 = pos_in_file (file , key)
  28.         num = dict1.key()
  29.         num = sorted(num)
  30.         for each in num:
  31.             print ('在有%s处,关键字位于%s,第%s个位置' % (file, num, dict1[num]))


  32. search_files('爱','G:/python/测试/30.4')
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-10-24 18:49:24 | 显示全部楼层    本楼为最佳答案   
print ('在有%s处,关键字位于%s,第%s个位置' % (file, num, dict1[num]))

少了个括号,补上就行了

  1. import os

  2. def pos_in_line (line,key):
  3.     pos= []
  4.     begin = line.find(key,0)
  5.     if begin != -1:
  6.         pos.append (begin+1)
  7.         begin = line.find(key,begin+1)
  8.     return pos
  9. def pos_in_file (file , key):
  10.     f = open (file)
  11.     count = 0
  12.     key_dict= dict()
  13.     for each_line in file:
  14.         count +=1
  15.         key_dict[count] = pos_in_line (each_line ,key)
  16.     f.close()
  17.     return key_dict

  18. def search_files(key,path):
  19.     txt_files=[]
  20.     dict1= dict()
  21.     for root,dirs, files in os.walk(path):
  22.         for name in files:
  23.             if os.path.splitext[1]== '.txt':
  24.                 txt_name = os.path.jion(root,name)
  25.                 txt_files.append (text_name)
  26.     for file in txt_files:
  27.         dict1 = pos_in_file (file , key)
  28.         num = dict1.key()
  29.         num = sorted(num)
  30.         for each in num:
  31.             print ('在有%s处,关键字位于%s,第%s个位置' % (file, num, dict1[num]))


  32. search_files('爱','G:/python/测试/30.4')
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-10-24 19:13:21 | 显示全部楼层
zltzlt 发表于 2019-10-24 18:49
print ('在有%s处,关键字位于%s,第%s个位置' % (file, num, dict1[num]))

少了个括号,补上就行了

哎呀太感谢了,谢谢谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-21 23:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表