Python;阅读文件并找到所需的文本 [英] Python; reading file and finding desired text

查看:49
本文介绍了Python;阅读文件并找到所需的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要创建一个具有两个参数的函数,一个要打开的文件名和一个模式.

Need to create a function with two params, a filename to open and a pattern.

该模式将是一个搜索字符串.

The pattern will be a search string.

例如.该函数将打开具有类似"The quick brown fox"(可能超过一行)之类的句子.txt

Eg. the function will open sentence.txt that has something like "The quick brown fox" (can possibly be more than one line)

图案将是棕狐"

因此,如果找到的话,它将返回行号和找到的字符串开始处的字符的索引.否则,返回-1.

So if found, as this will be, it should return a line number and index of the character the found string starts on. Else, return -1.

Catch是我以前从未用python编程过的,所以我不知道语法.以前使用C,C#,Java,VB等进行编码.

Catch is I've never programmed in python before so I don't know the syntax. Previously coded in C, C#, Java, VB, etc..

文件名= raw_input('输入文件名:')

filename = raw_input('Enter filename: ')

pattern = raw_input('输入模式:')

pattern = raw_input('Enter pattern: ')

def findPattern(fname,pat):

def findPattern(fname, pat):

filetext = open(fname).read()
if pat in filetext:
    print("Found it -- chunk")
else:
    print("Nothing -- chunk")

逐行阅读

for search in open(fname):
    if pat in search:
        print("Found it -- line")
    else:
        print("Nothing -- line")    

findPattern(文件名,模式)

findPattern(filename, pattern)

推荐答案

这是一个非常简单的grep.您可以轻松地使用正则表达式.使用 glob 进行遍历并不会困难得多.另外,您想要的代码位于 grep main 之间,因此可能比自定义grep更有趣;)

Here's a very simple grep. You could hack it out to use regular expressions pretty trivially. globbing wouldn't be much more difficult with glob. Also, the code you want is in there spread between grep and main so that might be of more interest than a custom grep ;)

def grep(filename, needle):
    with open(filename) as f_in:
        matches = ((i, line.find(needle), line) for i, line in enumerate(f_in))
        return [match for match in matches if match[0] != -1]

def main(filename, needle):
    matches = grep(filename, needle)
    if matches:
        print "{0} found on {1} lines in {2}".format(needle, len(matches), filename) 
        for line in matches:
            print "{0}:{1}:{2}".format(*line)
        return 1
    else:
        return -1

if __name__=='__main__':
    import sys
    filename = sys.argv[1]
    needle = sys.argv[2]
    return sys.exit(main(filename, needle))

请注意,我尚未测试此代码,因此可能存在一些错误.如果可以编译,则应该可以正常运行.

Note that I haven't tested this code so there might be slight bugs. If it compiles, it should run fine though.

此外,您应该告诉老师,用返回码表示失败是做事情的一种糟糕方式.如果要编写的函数的调用者需要知道是否未找到匹配项,则只需检查一个空列表即可.

Also, you should tell your teacher that signalling failure with return codes is a terrible way to do things. If the caller of the function that you're going to write needs to know if no matches were found, it can just check for an empty list.

这篇关于Python;阅读文件并找到所需的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆