I need to find a string inside of a file; I copied and pasted the exact string from the file into the pattern spot, but I still can't find it. The print
commands return empty strings, except for the first name. Here is my code:
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,'lastNam开发者_运维问答e')
family = re.findall(r'family\:', file.read())
print(family)
self.familyNameBox.insert(0,'family')
file.close()
And here are the contents of the file:
firstName: test
lastName: one
family: family
I think file.read() will move the cursor so subsequent calls will attempt to read from the end of the file.
You could read the contents of the file into a variable first, and then perform the regex searches in that.
Your better bet would be to read the lines of the file in using readlines() or xreadlines(). Something like this:
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
Also, if you want to use a regular expression for this, you'll want to use something like:
import re
line = "family: blah"
m = re.search("family: (.*)", line)
if m != None:
print m.group(1)
Mamma mia ! So much complications !
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')
By the way, what is the purpose of the instruction global fileDirectory
??
I suppose there are several sections like
firstName: test
lastName: one
family: family
so why not use a csv.reader() or csv.DictReader() ?
If you can make the file format a tad bit more flexible, you can use the built in Config Parser module (ConfigParser
in 2.X, configparser
in 3.x)
For the file:
[default]
firstName: test
lastName: one
family: family
The following code would work fine:
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')
EDIT Alternately, your original file could be parsed with csv as pointed out by 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]
精彩评论