• 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
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

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: 21719
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
 
Sheriff
Posts: 24374
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" )
 
no wonder he is so sad, he hasn't seen this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!