473,387 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

overriding character escapes during file input

Dear Python-list,

I need to read a Unicode (utf-8) file that contains text like:
blah \fR40\fC blah
I get my input and then process it with something like:
inputFile = codecs.open(sys.argv[1],'r', 'utf-8')

for line in inputFile:
When Python encounters the "\f" substring in an input line, it wants to
treat it as an escape sequence representing a form-feed control
character, which means that it gets interpreted as (or, from my
perspective, translated to) "\x0c". Were I entering this string myself
within my program code, I could use a raw string (r"\f") to avoid this
translation, but I don't know how to do this when I am reading a line
from a file. If all I cared about was getting my code to work, I could
simply let the translation take place and then undo it within my
program, but, as Humpty Dumpty said, "it's a question of which is to be
master," and I would prefer to coerce Python into reading the line the
way I want it to be read, rather than let it do as it pleases and then
clean up afterwards.

Can anyone advise?

In case it matters, I'm using ActivePython 2.4 under Windows XP.

Thanks,

David
dj************@pitt.edu
Sep 3 '06 #1
2 2571

David J Birnbaum wrote:
Dear Python-list,

I need to read a Unicode (utf-8) file that contains text like:
blah \fR40\fC blah
I get my input and then process it with something like:
inputFile = codecs.open(sys.argv[1],'r', 'utf-8')

for line in inputFile:
When Python encounters the "\f" substring in an input line, it wants to
treat it as an escape sequence representing a form-feed control
character,
Even if it were as sentient as "wanting" to muck about with the input,
it doesn't. Those escape sequences are interpreted by the compiler, and
in other functions (e.g. re.compile) but *not* when reading a text
file.

Example:
|>>guff = r"blah \fR40\fC blah"
|>>print repr(guff)
'blah \\fR40\\fC blah'
|>># above is ASCII so it is automatically also UTF8

Comment: It contains backslash followed by 'f' ...

|... fname = "guff.utf8"
|>>f = open(fname, "w")
|>>f.write(guff)
|>>f.close()
|>>import codecs
|>>f = codecs.open(fname,'r', 'utf-8')
|>>guff2 = f.read()
|>>print guff2 == guff
|True
No interpretation of the r"\f" has been done.
which means that it gets interpreted as (or, from my
perspective, translated to) "\x0c". Were I entering this string myself
within my program code, I could use a raw string (r"\f") to avoid this
translation, but I don't know how to do this when I am reading a line
from a file.
What I suggest you do is:
print repr(open('yourfile', 'r').read()
[or at least one of the offending lines]
and inspect it closely. You may find (1) that the file has formfeeds in
it or (2) it has r"\f" in in it and you were mistaken about the
interpretation or (3) something else.

If you maintain (3) is the case, then make up a small example file,
show a dump of it using print repr(.....) as above, plus the (short)
code where you decode it and dump the result.

HTH,
John

Sep 3 '06 #2

John Machin wrote:
David J Birnbaum wrote:
Dear Python-list,

I need to read a Unicode (utf-8) file that contains text like:
blah \fR40\fC blah
I get my input and then process it with something like:
inputFile = codecs.open(sys.argv[1],'r', 'utf-8')
>
for line in inputFile:
When Python encounters the "\f" substring in an input line, it wants to
treat it as an escape sequence representing a form-feed control
character,

Even if it were as sentient as "wanting" to muck about with the input,
it doesn't. Those escape sequences are interpreted by the compiler, and
in other functions (e.g. re.compile) but *not* when reading a text
file.

Example:
|>>guff = r"blah \fR40\fC blah"
|>>print repr(guff)
'blah \\fR40\\fC blah'
|>># above is ASCII so it is automatically also UTF8

Comment: It contains backslash followed by 'f' ...

|... fname = "guff.utf8"
|>>f = open(fname, "w")
|>>f.write(guff)
|>>f.close()
|>>import codecs
|>>f = codecs.open(fname,'r', 'utf-8')
|>>guff2 = f.read()
|>>print guff2 == guff
|True
No interpretation of the r"\f" has been done.
which means that it gets interpreted as (or, from my
perspective, translated to) "\x0c". Were I entering this string myself
within my program code, I could use a raw string (r"\f") to avoid this
translation, but I don't know how to do this when I am reading a line
from a file.

What I suggest you do is:
print repr(open('yourfile', 'r').read()
[or at least one of the offending lines]
and inspect it closely. You may find (1) that the file has formfeeds in
it or (2) it has r"\f" in in it and you were mistaken about the
interpretation or (3) something else.

If you maintain (3) is the case, then make up a small example file,
show a dump of it using print repr(.....) as above, plus the (short)
code where you decode it and dump the result.
================================================== =======
On 3/09/2006 3:53 PM, David J Birnbaum wrote in e-mail:
Dear John,

Thank you for the quick response. Ultimately I need to remap the "f" in
"\f" to something else, so I worked around the problem by doing the
remapping first, and I'm now getting the desired result.
Please reply on-list.

How could you read the file to remap an "f" if you were getting '\0x0C'
when you tried to read it? Are we to assume that it was case (2) i.e.
not a Python problem?

Cheers,
John

Sep 3 '06 #3

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

Similar topics

9
by: Christian Roth | last post by:
Hello, when using this "identity" processing sheet: <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" encoding="iso-8859-1" /> ...
5
by: Gary Mayor | last post by:
Hi, If I have the ' character within the javascript:pick command it doesn't work. Is there some sort of way of escaping these characters like in server side languages. function pick(symbol) {...
2
by: Rakesh Sinha | last post by:
Hi, I am writing this application in C++. It reads data from binary files. My current requirement is that: Given a positive number N, I have to read in N bytes from the input stream (which is...
1
by: Greg.Harabedian | last post by:
I'll start off by saying I am using MySQL v4.0 and my question is...how do I get mysqldump to dump the actual binary values store in a blob? Here is an example: -- Create a test table create...
2
by: Mike Turco | last post by:
I have a bunch of text files I'm trying to parse. The files all have several occurrences of chr(26), which is EOF (End Of File). Each file is ~ 1meg of text, and every file is a real mess. The...
7
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
6
by: Chris Anderson | last post by:
Anyone know of a fix (ideally) or an easy workaround to the problem of escape characters not working in regex replacement text? They just come out as literal text For example, you'd think that thi...
44
by: Kulgan | last post by:
Hi I am struggling to find definitive information on how IE 5.5, 6 and 7 handle character input (I am happy with the display of text). I have two main questions: 1. Does IE automaticall...
6
by: JKPeck | last post by:
I am trying to understand why, with nonwestern strings, I sometimes get a hex display and sometimes get the string printed as characters. With my Python locale set to Japanese and with or without...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...

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.