Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > unicode text file

Reply
Thread Tools

unicode text file

 
 
Junaid
Guest
Posts: n/a
 
      09-27-2009
I want to do replacements in a utf-8 text file. example

f=open("test.txt","r") #this file is uft-8 encoded

raw = f.read()
txt = raw.decode("utf-8")

txt.replace{'English', ur'ഇംഗ്ലീഷ്') #replacing raw unicode string,
but not working

f.write(txt)
f.close()
f.flush()


please, help me

thanks
 
Reply With Quote
 
 
 
 
Vlastimil Brom
Guest
Posts: n/a
 
      09-27-2009
2009/9/27 Junaid <>:
> I want to do replacements in a utf-8 text file. example
>
> f=open("test.txt","r") #this file is uft-8 encoded
>
> raw = f.read()
> txt = raw.decode("utf-8")
>
> txt.replace{'English', ur'ഇംഗ്ലീഷ്') #replacing raw unicode string,
> but not working
>
> f.write(txt)
> f.close()
> f.flush()
>
>
> please, help me
>
> thanks
> --
> http://mail.python.org/mailman/listinfo/python-list
>


Does
txt.replace('English', ur'ഇംഗ്ലീഷ്')
instead of
txt.replace{'English', ur'ഇംഗ്ലീഷ്')

fix the problem?

hth
vbr
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      09-27-2009
Junaid wrote:
> I want to do replacements in a utf-8 text file. example
>
> f=open("test.txt","r") #this file is uft-8 encoded
>
> raw = f.read()
> txt = raw.decode("utf-8")
>
> txt.replace{'English', ur'ഇംഗ്ലീഷ്') #replacing raw unicode string,
> but not working
>

txt = txt.replace{'English', ur'ഇംഗ്ലീഷ്')

> f.write(txt)
> f.close()
> f.flush()


The file will be flushed when it's closed, and flushing it after closing
is meaningless.
>
>
> please, help me
>
> thanks


 
Reply With Quote
 
Mark Tolonen
Guest
Posts: n/a
 
      09-27-2009

"Junaid" <> wrote in message
news:0267bef9-9548-4c43-bcdf-...
>I want to do replacements in a utf-8 text file. example
>
> f=open("test.txt","r") #this file is uft-8 encoded
> raw = f.read()
> txt = raw.decode("utf-8")


You can use the codecs module to open and decode the file in one step

>
> txt.replace{'English', ur'ഇംഗ്ലീഷ്') #replacing raw unicode string,
> but not working


The replace method returns the altered string. It does not modify it in
place. You also should use Unicode strings for both the arguments (although
it doesn't matter in this case). Using a raw Unicode string is also
unnecessary in this case.

txt = txt.replace(u'English', u'ഇംഗ്ലീഷ്')

> f.write(txt)


You opened the file for writing. You'll need to close the file and reopen
it for writing.

> f.close()
> f.flush()


Flush isn't required. close() will flush.

Also to have text like ഇംഗ്ലീഷ് in a file you'll need to declare the
encoding of the file at the top and be sure to actually save the file in the
encoding.

In summary:

# coding: utf-8
import codecs
f = codecs.open('test.txt','r','utf-8')
txt = f.read()
txt = txt.replace(u'English', u'ഇംഗ്ലീഷ്')
f.close()
f = codecs.open('test.txt','w','utf-8')
f.write(txt)
f.close()

-Mark


 
Reply With Quote
 
Junaid
Guest
Posts: n/a
 
      10-03-2009
On Sep 27, 6:39*pm, "Mark Tolonen" <metolone+gm...@gmail.com> wrote:
> "Junaid" <junu...@gmail.com> wrote in message
>
> news:0267bef9-9548-4c43-bcdf-...
>
> >I want to do replacements in a utf-8 text file. example

>
> > f=open("test.txt","r") #this file is uft-8 encoded
> > raw = f.read()
> > txt = raw.decode("utf-8")

>
> You can use the codecs module to open and decode the file in one step
>
>
>
> > txt.replace{'English', ur'ഇംഗ്ലീഷ്') #replacing raw unicode string,
> > but not working

>
> The replace method returns the altered string. *It does not modify it in
> place. *You also should use Unicode strings for both the arguments (although
> it doesn't matter in this case). *Using a raw Unicode string is also
> unnecessary in this case.
>
> * * txt = txt.replace(u'English', u'ഇംഗ്ലീഷ്')
>
> > f.write(txt)

>
> You opened the file for writing. *You'll need to close the file and reopen
> it for writing.
>
> > f.close()
> > f.flush()

>
> Flush isn't required. *close() will flush.
>
> Also to have text like ഇംഗ്ലീഷ് in a file you'll need to declare the
> encoding of the file at the top and be sure to actually save the file in the
> encoding.
>
> In summary:
>
> * * # coding: utf-8
> * * import codecs
> * * f = codecs.open('test.txt','r','utf-8')
> * * txt = f.read()
> * * txt = txt.replace(u'English', u'ഇംഗ്ലീഷ്')
> * * f.close()
> * * f = codecs.open('test.txt','w','utf-8')
> * * f.write(txt)
> * * f.close()
>
> -Mark


thanx everyone for replying,

I did as Mark suggested, and it worked

thanx once more
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Convert unicode escape sequences to unicode in a file Jeremy Python 0 01-11-2011 11:39 PM
Convert unicode escape sequences to unicode in a file Jeremy Python 1 01-11-2011 10:36 PM
Unicode digit to unicode string Gabriele *darkbard* Farina Python 2 05-16-2006 01:15 PM
unicode wrap unicode object? ygao Python 6 04-08-2006 09:54 AM
Unicode + jsp + mysql + tomcat = unicode still not displaying Robert Mark Bram Java 0 09-28-2003 05:37 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57