unicode text file

Discussion in 'Python' started by Junaid, Sep 27, 2009.

  1. Junaid

    Junaid Guest

    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
     
    Junaid, Sep 27, 2009
    #1
    1. Advertisements

  2. 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
     
    Vlastimil Brom, Sep 27, 2009
    #2
    1. Advertisements

  3. Junaid

    MRAB Guest

    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
     
    MRAB, Sep 27, 2009
    #3
  4. Junaid

    Mark Tolonen Guest

    "Junaid" <> wrote in message
    news:...
    >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
     
    Mark Tolonen, Sep 27, 2009
    #4
  5. Junaid

    Junaid Guest

    On Sep 27, 6:39 pm, "Mark Tolonen" <> wrote:
    > "Junaid" <> wrote in message
    >
    > news:...
    >
    > >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
     
    Junaid, Oct 3, 2009
    #5
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Krish
    Replies:
    1
    Views:
    1,219
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=
    Oct 20, 2005
  2. Guest
    Replies:
    0
    Views:
    352
    Guest
    Jan 19, 2005
  3. Eric Lilja
    Replies:
    8
    Views:
    828
    Eric Lilja
    Feb 22, 2005
  4. Koulbak

    unicode text file

    Koulbak, May 20, 2005, in forum: C++
    Replies:
    14
    Views:
    6,195
    Koulbak
    May 23, 2005
  5. Svennglenn

    Trouble saving unicode text to file

    Svennglenn, May 7, 2005, in forum: Python
    Replies:
    19
    Views:
    848
    Thomas Bellman
    May 11, 2005
  6. Chameleon

    load unicode text file, in J2ME

    Chameleon, Aug 6, 2006, in forum: Java
    Replies:
    3
    Views:
    10,297
    Darryl L. Pierce
    Aug 7, 2006
  7. walterbyrd
    Replies:
    7
    Views:
    519
    Asun Friere
    May 17, 2007
  8. Jeremy
    Replies:
    1
    Views:
    975
    Alex Willmer
    Jan 11, 2011
Loading...