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

Safe string escaping?

I have a data file that has lines like "foo\n\0" where the \n\0 is acutally
backslash+n+backslash+0. I.E. a repr of the string from python would be
"foo\\n\\0". I'm trying to convert this string into one that contains
actual newlines and whatnot. I feel like there has to be a better and safer
way to do this than eval("'%s'" % "foo\\n\\0") but I'm not finding it.

Any tips would be appreciated,

Grant

Jul 18 '05 #1
2 2436
Grant Olson wrote:
I have a data file that has lines like "foo\n\0" where the \n\0 is acutally
backslash+n+backslash+0. I.E. a repr of the string from python would be
"foo\\n\\0". I'm trying to convert this string into one that contains
actual newlines and whatnot.


r"foo\n\0".decode("string_escape")
--
Michael Hoffman
Jul 18 '05 #2
On Mon, 07 Mar 2005 19:24:43 -0500, "Grant Olson" <ol*****@verizon.net> wrote:
I have a data file that has lines like "foo\n\0" where the \n\0 is acutally
backslash+n+backslash+0. I.E. a repr of the string from python would be
"foo\\n\\0". I'm trying to convert this string into one that contains
actual newlines and whatnot. I feel like there has to be a better and safer
way to do this than eval("'%s'" % "foo\\n\\0") but I'm not finding it.

Any tips would be appreciated,
s = "foo\\n\\0"
s 'foo\\n\\0' list(s) ['f', 'o', 'o', '\\', 'n', '\\', '0'] sd = s.decode('string_escape')
sd 'foo\n\x00' list(sd) ['f', 'o', 'o', '\n', '\x00']

You could make a de-escaping utility function, e.g.,
def de_esc(s): return s.decode('string_escape') ... s 'foo\\n\\0' de_esc(s)

'foo\n\x00'

You might want to write a test to verify that it does what you
expect for the kind of escaped string you are using.

Regards,
Bengt Richter
Jul 18 '05 #3

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

Similar topics

11
by: Derek Fountain | last post by:
I was just writing a sanitisation route for a bit of user input. The data is an English text description of a product, and will go into a DB, then back out to other user's browsers. As per...
3
by: frizzle | last post by:
Hi there, I was wondering the folllowing: when i insert something into a mySQL DB -in a guestbook for instance- i mostly use mysql_escape_string($_POST['comment'). now i've seen...
1
by: shaun roe | last post by:
I have an xml format for creating a database; lets say a fragment looks like this: <insertValue type="int">7</insert> <insertValue type="string">Hello everyone</insert> now I have a...
4
by: joe_rattz | last post by:
I need to convert a text string ("Dewey & Cheatham & Howe") to an XML encoded string ("Dewey &amp; Cheatham &amp; Howe"). I am not building an XML document, I am just trying to convert a single string. I...
4
by: Guadala Harry | last post by:
I need to place the following into a string... How can I properly escape the % " / < and > characters? <table width="100%" border="0" cellspacing="0" cellpadding="4px" class="hfAll"></Table> ...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
5
by: archana | last post by:
Hi all, I want to apply regular expresion on string containing chatacters like '(,')',+' and many such which we are using while creating pattern for regulat expression. But when i am applying...
3
by: qilin | last post by:
I am trying to save a big text string into MySQL, but I guess i need escape the string firstly, anybody knows any escape function in c for that? or any other suggests ?
14
by: Arjen | last post by:
Hi, I have a string with this value 07/11/2008. As you see no time included. How can I parse this string to a datetime variable? I tried to do this but I get the exception that the format is...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?

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.