String Manipulation

M

Michael Jordan

hey, i have this huge text file and i need to go through and remove all
punctuation and every instance of the phrase "fruitloops=$" where $ is
any number 0-100" um, and yeah this is homework but i've tried to no
avail. thanks guys. cheerio :). jen
 
B

Bill Mill

hey, i have this huge text file and i need to go through and remove all
punctuation and every instance of the phrase "fruitloops=$" where $ is
any number 0-100" um, and yeah this is homework but i've tried to no
avail. thanks guys. cheerio :). jen

Jen,

This program iterates through one file and outputs all lines to
another file which have the word "homework" in them.

#-------------------------- Begin program 1
file_in = file('data.in')
file_out = file('data.out')

for line in file_in:
#line is a string containing one line of the file
if "homework" in line:
file_out.write("homework")
#--------------------------- End program 1

Here is a program which turns a string containing the phrase
"number=42" into a variable containing the integer 42:

#-------------------------- Begin program 2
#create a string variable called x
x = "number=42"

#split the string at the '=', resulting in ['number', '42']
n = x.split('=')[1]

#turn n from a string into a number, so we could test its value
n = int(n)

if 0 < n < 100:
print "n is between 0 and 100"
else:
print "n is not between 0 and 100"
#-------------------------- End program 2

And, finally, a program to remove punctuation from a string:

# ------------------------ Begin program 3
import string

#create a sentence with punctuation
punct = "This. is a, sentence with - punctuation"

#remove the punctuation; make sure the first argument
#to maketrans is the same length as the second, which
#should be all blanks
punct = punct.translate(string.maketrans('.,-', ' '))

#read the docs at
# http://docs.python.org/lib/node109.html
# for more details
#------------------------------------End program 3

Hope this helps; you should be able to put the pieces together to do
what you want to do. If you can't, feel free to ask more questions.
Also, just so you know, there is a list at (e-mail address removed) set up
just to answer questions like these.

Peace
Bill Mill
(e-mail address removed)
 

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,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top