Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Python to find all isbn in a text file?

Tags:

python

parsing

I have a text file text_isbn with loads of ISBN in it. I want to write a script to parse it and write it to a new text file with each ISBN number in a new line.

Thus far I could write the regular expression for finding the ISBN, but could not process any further:

import re
list = open("text_isbn", "r")
regex = re.compile('(?:[0-9]{3}-)?[0-9]{1,5}-[0-9]{1,7}-[0-9]{1,6}-[0-9]')

I tried to use the following but got an error (I guess the list is not in proper format...)

parsed = regex.findall(list)

How to do the parsing and write it to a new file (output.txt)?

Here is a sample of the text in text_isbn

Praxisguide Wissensmanagement - 978-3-540-46225-5
Programmiersprachen - 978-3-8274-2851-6
Effizient im Studium - 978-3-8348-8108-3
like image 945
mcbetz Avatar asked Jan 10 '13 13:01

mcbetz


1 Answers

How about

import re

isbn = re.compile("(?:[0-9]{3}-)?[0-9]{1,5}-[0-9]{1,7}-[0-9]{1,6}-[0-9]")

matches = []

with open("text_isbn") as isbn_lines:
    for line in isbn_lines:
        matches.extend(isbn.findall(line))
like image 189
Jakob Bowyer Avatar answered Oct 27 '22 12:10

Jakob Bowyer