[Tutor] Another parsing question
Jay Mutter III
jmutter at uakron.edu
Sat Mar 31 19:39:16 CEST 2007
I have the following that I am using to extract "numbers' from a file
prompt1 = raw_input('What is the file from which you would like a
list of patent numbers? ')
p1 = open(prompt1,'rU')
s = p1.readlines()
prompt2 = raw_input('What is the name of the file to which you would
like to save the list of patent numbers? ')
p2 = open(prompt2,'aU')
patno = re.compile(r'(\d{1})[\s,.]+(\d{3})[\s,.]+(\d{3})')
for line in s:
jay = patno.findall(line)
print jay
which yields the following
[('1', '337', '912')]
[('1', '354', '756')]
[('1', '360', '297')]
[('1', '328', '232')]
[('1', '330', '123')]
[('1', '362', '944')]
[('1', '350', '461')]
[('1', '355', '991')]
[('1', '349', '385')]
[('1', '350', '521')]
[('1', '336', '542')]
[('1', '354', '922')]
[('1', '338', '268')]
[('1', '353', '682')]
[('1', '343', '241')]
[('1', '359', '852')]
[('1', '342', '483')]
[('1', '347', '068')]
[('1', '331', '450')]
if i try to write to a file instead of print to the screen using
p2.write(jay)
i get the message
Traceback (most recent call last):
File "patentno.py", line 12, in ?
p2.write(jay)
TypeError: argument 1 must be string or read-only character buffer,
not list
I f I try writelines i get
Traceback (most recent call last):
File "patentno.py", line 12, in ?
p2.writelines(jay)
TypeError: writelines() argument must be a sequence of strings
jay-mutter-iiis-computer:~/documents/programming/python/patents jlm1$
So what do i have above ? A list of tuples?
How do I send the output to a file?
Is there a way to get the output as
1337912 instead of [('1', '337', '912')] ?
And as always thanks in advance for the help.
jay Mutter
More information about the Tutor
mailing list