简体   繁体   English

计算特殊字符在用户输入中出现在txt文件中的行数(Python 2.7)

[英]Counting the number of lines that special characters appear in a txt file from user input (Python 2.7)

Using Python 2.7, I am trying to create a program that simulates the grep search command in Unix. 使用Python 2.7,我试图创建一个程序来模拟Unix中的grep搜索命令。 In other words, I want to ask the user to enter a regular expression and then subsequently count the number of lines where the user's inputted regular expression appears in the file. 换句话说,我想请用户输入一个正则表达式,然后计算用户输入的正则表达式在文件中出现的行数。

This is my code which I already know is totally messed up (I've been at this problem for hours now, and I am at my wit's end). 这是我的代码,我已经知道它完全弄糟了(我已经在这个问题上待了好几个小时了,我已经不知所措了)。 In this code, I entered the string "^Author" which returned 0 lines when it should have returned approximately 1798 lines from the file that I have decided to open (the "what.txt" file): 在此代码中,我输入了字符串“ ^ Author”,该字符串应从我决定打开的文件(“ what.txt”文件)返回大约1798行时返回0行:

import re
hand = open('what.txt')
yo = raw_input("Enter a regular expression: ")
count = 0
for line in hand:
    x = re.findall('.*[a-zA-Z]+.*', line)
    if yo in line and len(x) > 0:
        count += 1

print "what.txt had", count, "lines that matched %s" % yo

I am drawing a blank and haven't been able to find answers relating to this problem on StackOverflow. 我正在绘制空白,但无法在StackOverflow上找到与此问题有关的答案。 In short, any help would be awesome. 简而言之,任何帮助都会很棒。

You're not actually using your regex in your search at the moment. 目前,您实际上并未在搜索中使用正则表达式。

x = re.findall(yo, line)
if x:
    count += 1 # multiple non-overlapping occurences on one line

print "what.txt had {0} lines that matched {1}".format(count, yo)

How about using re module to handle the regex? 如何使用re模块来处理正则表达式?

for line in hand.readlines():
    if re.findall(yo,line):
        count+=1

In your code you're using a regex as if it was just a string, but you have to use a module that works with regexes, such as re . 在您的代码中,您使用的是正则表达式,就好像它只是一个字符串一样,但是您必须使用与正则表达式兼容的模块,例如re

It seems to me like you should be passing the expression you gather into yo to re.findall() . 在我看来,像你应该通过您收集的表达式转换yore.findall() I'm not that familiar with grep, but just passing that to findall seems to work for me. 我对grep不太熟悉,但是将它传递给findall似乎对我有用。

import re
hand = open('what.txt')
yo = raw_input("Enter a regular expression: ")
count = 0
for line in hand:
    x = re.findall(yo, line)
    if len(x) > 0:
        count += 1
        print(x)

print "what.txt had", count, "lines that matched %s" % yo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM