正则表达式的麻烦

时间:2011-02-24 16:09:14

标签: python regex file-io

我需要在文件中找到一个字符串;我将文件中的确切字符串复制并粘贴到模式点,但我仍然无法找到它。 print命令返回空字符串,第一个名称除外。这是我的代码:

def fillWindow(self,student):
    global fileDirectory
    location = path.join(fileDirectory, student + '.txt')
    file = open(location, 'r')

    # find item in list and then place it in the text box   
    firstName = re.findall(r'firstName\:', file.read())
    print(firstName)
    self.firstNameBox.insert(0,'firstName')

    lastName = re.findall(r'lastName\:', file.read())
    print(lastName)
    self.lastNameBox.insert(0,'lastName')

    family = re.findall(r'family\:', file.read())
    print(family)
    self.familyNameBox.insert(0,'family')

    file.close()

以下是文件的内容:

firstName: test
lastName: one
family: family

4 个答案:

答案 0 :(得分:5)

我认为file.read()会移动光标,以便后续调用尝试从文件末尾读取。

您可以先将文件内容读入变量,然后在其中执行正则表达式搜索。

答案 1 :(得分:1)

更好的选择是使用readlines()或xreadlines()读取文件的行。像这样:

input = open('input')

for line in input.xreadlines():
  if line.startswith('firstName'):
    firstname = line.split()[1]
  elif line.startswith('lastName'):
    lastname = line.split()[1]
  elif line.startswith('family'):
    family = line.split()[1]

input.close()

print firstname
print lastname
print family

另外,如果你想为此使用正则表达式,你会想要使用类似的东西:

import re
line = "family: blah"
m = re.search("family: (.*)", line)
if m != None:
  print m.group(1)

答案 2 :(得分:0)

如果您可以使文件格式更加灵活,可以使用内置的Config Parser模块(2.X中的ConfigParser,3.x中的configparser

对于文件:

[default]
firstName: test
lastName: one
family: family

以下代码可以正常工作:

import ConfigParser

def fillWindow(self,student):
    global fileDirectory
    location = path.join(fileDirectory, student + '.txt')
    parser = ConfigParser.ConfigParser()
    parser.read(location)
    firstName = parser.get('default','firstName')
    lastName = parser.get('default','lastName')
    family = parser.get('default','family')

编辑或者,您可以使用cyv解析原始文件,如eyquem所指出的那样:

import csv

def fillWindow(self,student):
    global fileDirectory
    location = path.join(fileDirectory, student + '.txt')
    reader = csv.reader(open(location),delimiter=':')
    dict = {}
    for row in reader:
        dict[row[0]] = row[1]

答案 3 :(得分:0)

妈妈咪!这么多并发症!

def fillWindow(student):
    global fileDirectory
    location = path.join(fileDirectory, student + '.txt')

    pat = re.compile('firstName: (.*)\r?\n'
                     'lastName: (.*)\r?\n'
                     'family: (.*)')

    with open(location, 'r') as f:
        ch = f.read()

        firstName,lastName,family = pat.search(ch).groups()
        # or li = pat.findall(ch) if there are several

        print(firstName)
        self.firstNameBox.insert(0,'firstName')
        print(lastName)
        self.lastNameBox.insert(0,'lastName')
        print(family)
        self.familyNameBox.insert(0,'family')

顺便说一句,指令的目的是什么global fileDirectory ??

我想有几个部分,比如

firstName: test
lastName: one 
family: family

那么为什么不使用 csv.reader() csv.DictReader()