Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > String Manipulation

Reply
Thread Tools

String Manipulation

 
 
Michael Jordan
Guest
Posts: n/a
 
      07-13-2005
i'll be straight with you and say that this is a homework assignment.
ive tried to figure it out on my own but am now out of time.

i need to go through a .txt file and get rid of all punctuation. also,
every time i see the work "Fruitloops=1" or "Hamburgers=x" where x is
ANY number i need to get rid of that also. thanks a bunch. hurry
please!

jen

 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      07-13-2005
Use .replace function to replace punctuation (you didn't say
exactly what that means but just to get you started):

#
# Extend this list as needed
#
punctuations=',.;)'
#
# Open input and output files
#
ifp=open(inputfilename,'r')
ofp=open(outputfilename,'w')
#
# Strip out the punctuation characters
#
for line in ifp:
for punctuation in punctuations:
line=line.replace(punctuation,'')
ofp.write(line)

#
# I'll leave the other part for homework but
# you will need to use the .find method of the string
#

ifp.close()
ofp.close()

Larry Bates


Michael Jordan wrote:
> i'll be straight with you and say that this is a homework assignment.
> ive tried to figure it out on my own but am now out of time.
>
> i need to go through a .txt file and get rid of all punctuation. also,
> every time i see the work "Fruitloops=1" or "Hamburgers=x" where x is
> ANY number i need to get rid of that also. thanks a bunch. hurry
> please!
>
> jen
>

 
Reply With Quote
 
 
 
 
Josef Meile
Guest
Posts: n/a
 
      07-13-2005
Hi,

> for punctuation in punctuations:
> line=line.replace(punctuation,'')

I would use maketrans or even a regex instead. However, If you care
about speed, it is well known that in some cases regex take more
time than multiple replaces. Even the maketrans could take more time
(I don't know; you may benchmark it -> homework)

Regards,
Josef

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String manipulation Chris ASP .Net 1 05-24-2004 02:19 PM
Advanced String Manipulation (C#) Aaron ASP .Net 2 01-02-2004 06:50 PM
String Manipulation Aaron ASP .Net 3 12-31-2003 03:39 PM
Perl string manipulation jared in ecs Perl 2 10-22-2003 05:36 PM
String Array Manipulation Problem Garfield ASP .Net 5 08-27-2003 10:07 PM



Advertisments