• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Replace special characters

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I start in Jython and I would like open a file and replace special characters

Here my code :
s = open("C:\JYTHON\TEST.xml").read()
s = s.replace('&', '')
s = s.replace('è', '')
s = s.replace('é', '')
s = s.replace('*', '')
s = s.replace('%', '')
s = s.replace('@', '')
s = s.replace('ç', '')
s = s.replace('à', '')
s = s.replace('#', '')
s = s.replace('«', '')
s = s.replace('»', '')
f = open("C:\JYTHON\TEST.xml", 'w')
f.write(s)
f.close()

It works for : & * %  #
It does not work for : é è ç à << >>


In addition, my list may not be exhaustive: ê Ê ...

Thank you in advance for your help
 
Sheriff
Posts: 21774
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which Python version are you using? If you're using Python 2, strings are not automatically Unicode. Try using s = s.replace(u'ç', '') - the u tells Python that it's a Unicode string.
 
Peter Boyer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:Which Python version are you using? If you're using Python 2, strings are not automatically Unicode. Try using s = s.replace(u'ç', '') - the u tells Python that it's a Unicode string.



Hi,
Thank you for your response.
I use Jython and not Pyhton from the ODI software, maybe you know?

Nevertheless, I tried s = s.replace(u'ç', '') but without sucess
 
Sheriff
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume that when you say "doesn't work" that you actually mean that the characters in question are not removed from the text.

Anyway I'm surprised that the "@" character is one of those so-called "special" characters which isn't handled correctly. That means it's not a Unicode or character set issue because that's an ordinary ASCII character.

However since I'm no Jython (or Python) expert I can't suggest anything more.
 
Peter Boyer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:I assume that when you say "doesn't work" that you actually mean that the characters in question are not removed from the text.

Anyway I'm surprised that the "@" character is one of those so-called "special" characters which isn't handled correctly. That means it's not a Unicode or character set issue because that's an ordinary ASCII character.

However since I'm no Jython (or Python) expert I can't suggest anything more.



Ohh you are right, it was a mistake, it is ok for @.
I wanted write that it does not work for à
And indeed when i wrote "does not work", that means that the characters in question are not removed from the text
 
Peter Boyer
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Peter Boyer wrote:

Paul Clapham wrote:I assume that when you say "doesn't work" that you actually mean that the characters in question are not removed from the text.

Anyway I'm surprised that the "@" character is one of those so-called "special" characters which isn't handled correctly. That means it's not a Unicode or character set issue because that's an ordinary ASCII character.

However since I'm no Jython (or Python) expert I can't suggest anything more.



Ohh you are right, it was a mistake, it is ok for @.
I wanted write that it does not work for à
And indeed when i wrote "does not work", that means that the characters in question are not removed from the text



It is OK :)
FYI, we have to add : s = unicode( t, "utf-8" )
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!