473,479 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Regular expressions and Python

1 New Member
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?
Mar 23 '08 #1
2 1341
bvdet
2,851 Recognized Expert Moderator Specialist
Please use code tags when posting code. Posting Guidelines - How to ask a question
You do not need regular expressions for this. Create an empty list to hold the results. Split the string, append the processed results to the list.
Expand|Select|Wrap|Line Numbers
  1. f = open('file_name')
  2. output = []
  3. for line in f:
  4.     lineList = line.split()
  5.     output.append(['%s%s' % (lineList[0][0].lower(), lineList[1].lower()), \
  6.                    '_'.join([word.lower() for word in lineList[2:]])])
  7.  
  8. f.close()    
  9.  
  10. for item in output:
  11.     print item
  12.  
>>> ['psmith', 'the_lobster_story']
['cbower', 'in_the_closet']
['tmartin', 'how_to_paint_your_furniture']
>>>
Mar 23 '08 #2
bvdet
2,851 Recognized Expert Moderator Specialist
I modified your regex pattern somewhat and added names to the groups. Using names, it is easier to read the structure of the pattern, and you can access the matched substrings with the MatchObject.groupdict() method. The rest is almost the same code as the non-regex solution.
Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. rgx = re.compile(r'%s %s %s' % ('(?P<first_name>(?P<first_initial>[A-Z])?\w+)', \
  4.                                 '(?P<last_name>(?P<last_initial>[A-Z])?\w+)', \
  5.                                 '(?P<book_title>.+)'))
  6. f = open(fn)
  7. output = []
  8. for line in f:
  9.     m = rgx.search(line)
  10.     dd = m.groupdict()
  11.     output.append(['%s%s' % (dd['first_initial'].lower(), dd['last_name'].lower()), \
  12.                    '_'.join([word.lower() for word in dd['book_title'].split()])])
  13.  
  14. f.close()    
  15.  
  16. for item in output:
  17.     print item 
>>> ['psmith', 'the_lobster_story']
['cbower', 'in_the_closet']
['tmartin', 'how_to_paint_your_furniture']
>>>
Mar 23 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
4154
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
19
2284
by: Davy | last post by:
Hi all, I am a C/C++/Perl user and want to switch to Python (I found Python is more similar to C). Does Python support robust regular expression like Perl? And Python and Perl's File...
34
1805
by: Antoine De Groote | last post by:
Hello, Can anybody tell me the reason(s) why regular expressions are not built into Python like it is the case with Ruby and I believe Perl? Like for example in the following Ruby code line =...
20
3365
by: Geoff Hill | last post by:
What's the way to go about learning Python's regular expressions? I feel like such an idiot - being so strong in a programming language but knowing nothing about RE.
13
7456
by: Wiseman | last post by:
I'm kind of disappointed with the re regular expressions module. In particular, the lack of support for recursion ( (?R) or (?n) ) is a major drawback to me. There are so many great things that can...
0
7027
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6899
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7067
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6847
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4463
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2970
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
555
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
166
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.