473,383 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Help with mass remove in text file

I'm trying to open a text file, remove all instances of the words
"f=x;" and "i=x;" where x can be any number 0-14. Also, I want to
remove all { " or ) or ( or ' } each time one of those characters
occurs respectively. This is what I've been able to piece together...
import os, string
x = ("f=;")
y = ("i=;)
inputFile = open('abcd.txt','r')
data = inputFile.read()
inputFile.close()
search = string.find(data, x)
if search >=1:
data = data.replace(x)
data = data.replace(y)
outputFile = open('abcd.txt','w')
outputFile.write(data)
outputFile.close()
This doesn't work, even to just remove "f=;". Any help would be great.

Thanks,
Reece

Jul 21 '05 #1
2 2083
First, in your intro you say you want to remove all strings of the form
"f=n;" where n can be 0-14. So you want to remove "f=0;" and "f=1;" and
.... Later, you appear to be trying to remove "f=;" which may be what
you want but it doesn't match your described intentions.

Second, the formatting (whitespace) is all messed up on your post (at
least in googroups), so its not entirely clear what the program is
supposed to do.

Anyway, why do you say the program doesn't work? Does it generate an
error when you try to run it? Or is it doing what you say and not what
you mean?

I'm guessing that it will give (provided it doesn't end on a
SyntaxError) at least the following error:
TypeError: replace() takes at least 2 arguments (1 given)

This is because the line:
data = data.replace(x)

doesn't use enough arguments for the replace() method. You need to say
what to replace it with. Since you want to remove whatever string it
is, you can use the empty string as the replacement value:

data = data.replace(x, '') #That's two single quotes

Also, unless you want to make a regular expression, you might have to
do a replace() call on data 15 times, sort of like this:
for i in range(15):
text_to_remove = "f=%s;" % i
data = data.replace(text_to_remove, '')

Jul 21 '05 #2
ro****@gmail.com wrote:
I'm trying to open a text file, remove all instances of the words
"f=x;" and "i=x;" where x can be any number 0-14. Also, I want to
remove all { " or ) or ( or ' } each time one of those characters
occurs respectively. This is what I've been able to piece together...


See the thread titled "String Manipulation" which your classmate "jen"
started, which has several useful answers already.

Hopefully others will note that this is homework as well and not totally
hand you the answer on a platter, so you'll actually learn something.
Kudos to you for actually attempting it and posting your code though. ;-)

-Peter
Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: da Vinci | last post by:
OK, this has got to be a simple one and yet I cannot find the answer in my textbook. How can I get a simple pause after an output line, that simply waits for any key to be pressed to move on? ...
4
by: Richard Tierney | last post by:
To create help output (the response to "myprog --help", for example) I currently create a big .h file, which includes a single string, such as: static char *help_text = "\ myprog: my program\n\...
3
by: Strasser | last post by:
In Access2000 mass emailing worked perfectly (very powerful tool!). Doesn't work when using XP version of both Access and Outlook, even though I checked the box to ensure that I was sending the...
6
by: mail.cordon | last post by:
Hi everyone, I need an algorithm/program that can computer planetory position data for a given date/time/place. Can anyone please help me to find the same (source code/algorithm) or explain me...
8
by: Roger Dodger | last post by:
I have to send out 2500 e-mails to student survey recipients. The e-mails will appear to come from the specific departments that they belong to. I'm utilizing PHPmailer in the scripts I wrote to...
1
by: teo | last post by:
Hallo, I'm performing a mass insertion from a text file to a db table with AdoNet commands, like this: myCommand.CommandText = "BULK INSERT ..." from a Win form no problem
4
by: joder2006 | last post by:
So I'm working on this class and I have a major problem. This class happens to be for a rocket, it's a game that you play and you try to see it you can land safely from 1000 meters up in the...
0
by: nt91rx78 | last post by:
Our college changes 18 weeks semester to 16 semester, so our CS professor cannot finish teaching the last important chapter which is related with my problw\em. This is program C problem Anyone...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.