473,738 Members | 3,636 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

converting octal strings to unicode

I have several ascii files that contain '\ooo' strings which represent
the octal value for a character. I want to convert these files to
unicode, and I came up with the following script. But it seems to me
that there must be a much simpler way to do it. Could someone more
experienced suggest some improvements?

I want to convert a file eg. containing:

hello \326du

with the unicode file containing:

hello du
----------8<---------------------------------------
#!/usr/bin/python

import re, string, sys

if len(sys.argv) > 1:
file = open(sys.argv[1],'r')
lines = file.readlines( )
file.close()
else:
print "give a filename"
sys.exit()

def to_unichr(str):
oct = string.atoi(str .group(1),8)
return unichr(oct)

for line in lines:
line = string.rstrip(u nicode(line,'La tin-1'))
if re.compile(r'\\ \d\d\d').search (line):
line = re.sub(r'\\(\d\ d\d)', to_unichr, line)
line = line.encode('ut f-8')
print line

----------8<---------------------------------------

Jul 18 '05 #1
2 4282
On 23 Dec 2004 18:41:57 -0800, rumours say that fl************@ gmail.com
might have written:
I have several ascii files that contain '\ooo' strings which represent
the octal value for a character. I want to convert these files to
unicode, and I came up with the following script. But it seems to me
that there must be a much simpler way to do it. Could someone more
experienced suggest some improvements?


decoded_string = "\326du".decode ("string_escape ")
unicode_text = unicode(decoded _string, "latin-1")
--
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
Jul 18 '05 #2
On 23 Dec 2004 18:41:57 -0800, rumours say that fl************@ gmail.com
might have written:
I have several ascii files that contain '\ooo' strings which represent
the octal value for a character. I want to convert these files to
unicode, and I came up with the following script. But it seems to me
that there must be a much simpler way to do it. Could someone more
experienced suggest some improvements?


(hope I cancelled the previous off-by-one-backslash post...)

your_string = "\\326du"
decoded_string = your_string.dec ode("string_esc ape")
unicode_text = unicode(decoded _string, "latin-1")
--
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
2205
by: Sean McIlroy | last post by:
I recently found out that unicode("\347", "iso-8859-1") is the lowercase c-with-cedilla, so I set out to round up the unicode numbers of the extra characters you need for French, and I found them all just fine EXCEPT for the o-e ligature (oeuvre, etc). I examined the unicode characters from 0 to 900 without finding it; then I looked at www.unicode.org but the numbers I got there (0152 and 0153) didn't work. Can anybody put a help on me wrt...
10
8534
by: Maxim Kasimov | last post by:
there are a few questions i can find answer in manual: 1. how to define which is internal encoding of python unicode strings (UTF-8, UTF-16 ...) 2. how to convert string to UCS-2 (Python 2.2.3 on freebsd4) -- Best regards, Maxim
8
3873
by: Alphaboomer | last post by:
I'm using the following code to retrieve a list of all the Categories used by Microsoft Outlook: sub test() Dim objWSHShell As Object Dim strCategoryList As Variant Set objWSHShell = CreateObject("WScript.Shell") strCategoryList = objWSHShell.RegRead_ ("HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Outlook\Categories\MasterList")
2
3545
by: Gidi | last post by:
Hi, I'm writing a C# win application program, and i need to transfer my hebrew letters from unicode to ascii, now if i use the ascii encoding it writes me ??? instead of the hebrew letter i've entered. I know what the Ascii value of each letter, so i understood that i can transfer my string to BYTE and enter the ascii value by myself. if someone has a better idea, i'll be happy to hear about it. how can i know the Unicode value of a...
9
5675
by: Hugo Amselschlag | last post by:
Hi there, I've implemented a local system hook to suppress certain windows beeing displayed by the axWebbrowser control. Now I need some more information before I can decide, whether to suppress a window or not. My callback function get a long pointer (lParam) to a structure which contains further information. This structure is described at MSN as follows: typedef struct tagCREATESTRUCT {
3
4941
by: bolly | last post by:
Hi, I've been putting Python data into a sqlite3 database as tuples but when I retrieve them they come back as unicode data e.g 'u(1,2,3,4)'.How can I change it back to a tuple so I can use it as a Python native datatype? I have looked in the docs and seen there is a decode/encode method but how do I do this? Hope you can help.
15
35508
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
2
5384
by: Nikola Skoric | last post by:
What I have is a bunch of text in arabic, and series of Unicode bytes which represent those arabic words (like this: \'c2\'e4\'f6\'d3\'f3\'c9 \'f1). Now I have to figure out how to convert my arabic text to bunch of \'somethings. If I understood Unicode correctly (and I'm not sure if I did), I first have to figure out which encoding this is (UTF-16 or UTF-32 or some other) and then convert the letters to their byte representation. I think...
9
11570
by: Michael Goerz | last post by:
Hi, I am writing unicode stings into a special text file that requires to have non-ascii characters as as octal-escaped UTF-8 codes. For example, the letter "Í" (latin capital I with acute, code point 205) would come out as "\303\215". I will also have to read back from the file later on and convert the escaped characters back into a unicode string.
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.