Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • 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
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Replace special characters  RSS feed

 
Greenhorn
Posts: 5
  • 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: 21747
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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: 5
  • 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
 
Marshal
Posts: 24498
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: 5
  • 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: 5
  • 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" )
 
Then YOU must do the pig's work! Read this tiny ad. READ IT!
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!