Hello,
I am working with Regular Expressions in Python.
I have a text file (authors.txt) file that contains the first and last name of an author separated with a whitespace, then a whitespace and the book title:
Peter Smith The Lobster story
Christine Bower In the closet
Tom Martin How to paint your furniture
My questions:
1) I want to transform the name into a string like this:
Peter Smith => psmith
I tried to get the first character of the first group, then to concatenate it with the second group and transform the whole string into lower case.
2.) Then I want to transform the book title into a string like this:
The Lobster story => the_lobster_story
I guess I just have to replace the whitespaces with an underscore '_' and transform the whole thing into lowercase but I don't know what function to use and how...
Then I wrote this script.py:
import re
import string
rgx = re.compile("(([A-Z])+\w+)[ ](([A-Z])+\w+)[ ]([^:]+)")
inf = open('authors.txt', 'r')
outf= open('authors2.txt', 'w')
for row in inf.readlines():
corr = rgx.search(row)
#these variables are false
first_name = corr.group(0)
last_name = corr.group(1)
name = corr.lower(first_name[0])+corr.lower(last_name)
title = corr.group(2)
title2 = title.lower(title.replace(' ', '_'))
if corr != None:
str = name ,"@bookstore.com" + " " + http://www.bookstore.org/", title2
outf.write(str)
inf.close()
outf.close()
Can anyone help?