String Manipulation

M

Michael Jordan

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 :)
 
L

Larry Bates

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
 
J

Josef Meile

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top