473,386 Members | 1,799 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,386 software developers and data experts.

How to replace several different characters in text file using asterisk in python?

I try to delete from text file those characters '{a}' '{b}' ... and so on (i have 250 curly braces in the text file) using this code:

Expand|Select|Wrap|Line Numbers
  1. # -*- coding: cp1255 -*-
  2. import sys,codecs,string
  3.  
  4. reload(sys)
  5. sys.setdefaultencoding('utf8')
  6. root = r"G:\desktop\y\test2.txt"
  7. x = open(root)
  8. s=x.read().replace('{*}','').replace('-','')
  9. x.close()
  10. x=open(root,"w")
  11. x.write(s)
  12. x.close
because the letters change in every curly brackets i used asterisk in the,

but after i run this code nothing change in the text file:

>>> ================================ RESTART ================================
>>>
>>>
Jan 21 '20 #1
2 1368
Luuk
1,047 Expert 1GB
It's not clear what you are trying to do.

Can you provide a minimum example of you filterfile (test2.txt), and and what you expect as output?
Jan 21 '20 #2
SioSio
272 256MB
How about using "re.sub"?
Here is an example to delete characters '{a}' '{b}' '-'
Expand|Select|Wrap|Line Numbers
  1. import re
  2. text = "aaaaaa{a}-bbbbbbb{b}-ccccc{c}-dddddd{d}."
  3. text_mod = re.sub('\{[a-b]\}','',text).replace('-','')
  4. print (text_mod)
  5.  
result :aaaaaabbbbbbbccccc{c}dddddd{d}.
Jan 22 '20 #3

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

Similar topics

2
by: Simon Verona | last post by:
Not sure if this is the best group... it may be better off in one of the ADO groups, but I'm sure somebody here knows the answer: I'm trying to load up a text file using ADO.net, as follows: ...
2
by: Praveen_db2 | last post by:
Hi all Db2 8.1.3 windows Is there any way to write data into a text file using a stored procedure? The way we return a cursor output to the calling application, can we return data in a text...
1
by: christine0207 | last post by:
Hi, how to generate a fixed length data text file using Scripting.TextStream / filesystemobject.
2
by: asenthil | last post by:
Hai, i'm a beginner to java... just now i had tried to read and write files using java... and then i had tried to connect a database using jdbc... now i want to export the data's from a...
4
by: varsha desai | last post by:
Hello there, I want to change some data(which is in one line only) of text file using VB 6.0. Which is the best method for it? Another question is I want to delete last two, three lines...
2
by: charlesbritto | last post by:
A C++ program for counting individual string in a text file using file handling functions., for ex: if the text file contains, am a boy,am studying +2,am from chennai Now the result shoud...
2
by: sajidali | last post by:
i am trying to append a text file using c#. when i executes my application i writes to the text file using more then one functions. i want to append file only during execution of programe. next time...
3
by: tivicoo | last post by:
I have a 16 bit array called ADData, and i want to save this array to a text file using C#. i tried to use different codes that i located online but when i open the saved data on notepad the data...
0
by: Thiem Teddy | last post by:
Dear all I use VB6 to export data into xls or text file, I followed this example ( http://bytes.com/topic/visual-basic/answers/530866-how-export-table-data-into-xls-text-file-using-vb6 ). but I...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.