Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RE function
#11

ok so now I have the following code, but the output is a set of brackets.

import re
str = open('directory', 'r').read()
pattern = re.findall('\d+(M)\d+(N)\d+(M)', 'filename')
print pattern
output
[]
Reply
#12
First, str is a reserved word. Don't use it as a variable name. Second, the variable should be the second part in findall. 'filename' is a string. It has no connection to anything else. When you are putting it in findall, you are searching the literal text 'filename', not any file you've opened. You want something more like:

import re
text = open('directory').read()
results = re.findall('\d+(M)\d+(N)\d+(M)', text)
print results
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#13
Oh I got my answer. Thank you.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020