473,383 Members | 1,997 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,383 software developers and data experts.

constructing binary \n

Is there a more elegant way to construct \[a-z] in a string than
something like:

s = '\\n'
result = eval( "'%s'" ) % s

Another ugly method would be to build a dict with all the different
special letters I want as keys, and their corresponding values as
values. Or I could have a huge if/elif structure. I can't make ord
work, because while ord( '\n' ) gives me a reasonable integer that I
can interpolate with %c, I don't have '\n', I have '\\n'.

Is there a simple, graceful way to do this sort of translation?

steve

Jul 18 '05 #1
1 1372
Steven Arnold wrote:
Is there a more elegant way to construct \[a-z] in a string than
something like:

s = '\\n'
result = eval( "'%s'" ) % s

Another ugly method would be to build a dict with all the different
special letters I want as keys, and their corresponding values as
values. Or I could have a huge if/elif structure. I can't make ord
work, because while ord( '\n' ) gives me a reasonable integer that I
can interpolate with %c, I don't have '\n', I have '\\n'.

No, you actually *do* have '\n', the single byte that represents ASCII
linefeed.
len('\n') 1 len('\\n') 2 ord('\n') 10 ord('\\n') Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: ord() expected a character, but string of length 2 found for char in '\\n': .... print ord(char)
....
92
110


Note that '\n' is a single byte, while '\\n' is two bytes. In the first
case, '\n' is interpreted as the single LF byte. In the second case,
'\\' collapses into a single backslash, giving you a backslash byte and
a 'n' byte.

When you type a string literal containing a backslash, if that backslash
can combine with the following character to make a valid escape code, it
*will* do so unless you've explicitly turned off escaping (by, e.g.,
using raw strings). Of course, if the combination is *not* a valid
escape code, then the backslash and following character will be
interpreted normally.

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #2

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

Similar topics

0
by: Krasimir_Slaveykov | last post by:
Hello , I have a problem with constructing correct SQL. I have tables: TABLE1 wich have FIELD1 and FIELD2 TABLE2 wich have FIELD1 and FIELD2 TABLE3 wich have FIELD1, FIELD2 and FIELD3 I...
3
by: karthick | last post by:
Hi, I am constructing a Message (Body) for sending our Emails. It is around 3000 characters long. But for whatever reason, the last line seems to be broken with a "!" exclamatory mark in it,...
1
by: cainlevy | last post by:
Hey all, What are the pros and cons of defining methods in the constructor vs through the prototype? For example: Constructing: ------------- function MyObj() { this.MyMethod = function()...
19
by: Michael Schuerig | last post by:
In my experience most available books and web publication on CSS are related to web page/site design. Little attention is payed to the design and construction of web-based application for data...
7
by: Hyoung Lee | last post by:
A simple method of simulating a class, such as C++ or UML class, in C would be using the struct construct. Similarly, we use C functions to simulate a methods; e.g., rv method_a (&struct,...
5
by: grocery_stocker | last post by:
Why would someone go through the trouble of constructing an error handling function using variable-length argument lists? Why not just use something like printf()?
1
by: Gravy | last post by:
Hi, When I create a new reference to a webservice proxy class (one that was created when I imported a web reference) there is a slight delay. Can someone tell me what it's doing to cause this...
11
by: bgreen | last post by:
I am hoping for some assistance with formatting a large text file which consists of a series of individual records. Each record includes specific labels/field names (a sample of 1 record (one of...
8
by: mvkml | last post by:
hi! I am working with bmp files for my project.I am having the pixel data (binary.. i.e;10001001001..)and i need to represent it as a .bmp file.I need to construct an image(.bmp) from this data...
1
by: katdaniel16 | last post by:
how to construct a binary tree from inorder and postorder using the c programming language.. pseudocode will do.. thanks
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.