Python Forum
string conversion like in source code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
string conversion like in source code
#1
in Python source code, strings can be written with certain backslash codes like 'foo\nbar' will have a len() of 7 and print with 'foo' and 'bar' split to separate lines.  ord('foo\nbar'[3]) and ord('\n') will both be 10.

what i would like to know is how to do the same conversions as the Python interpreter will do when processing strings like that in source code, such as if you Python code needs to read in some Python source code an do the same with a string it gets.  also, how would you do the reverse, such as the need to convert a string with things like a newline and a carriage return in them, to a string ready to be placed between quotes and output as part of some Python source?

i can already think of ways to do this.  but i am wanting to know how you would do it.  i would like to see what is the most pythonic way or if there is a function in some module that will just do it.  code that can handle bytes and bytearray, working under python3, would be even better.  bode that can do all under both python2 and python3 would be best.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
You could use the six package:
http://six.readthedocs.io/#binary-and-text-data
Reply
#3
(Nov-25-2017, 05:46 AM)heiner55 Wrote: You could use the six package:
http://six.readthedocs.io/#binary-and-text-data

looking through the documentation, i don't see which function(s) to use for this.

(Nov-25-2017, 05:46 AM)heiner55 Wrote: You could use the six package:
http://six.readthedocs.io/#binary-and-text-data

i am guessing you meant this for my "string conversion like in source code" thread and accidentally posted it here.  if posting here was indented because it has something for this, could you narrow down what it is?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
Opps, you are right.
Reply
#5
and my oops, too.  i meant to say "chr() for bytes" to refer to the other thread.  this thread is "string conversion like in source code"
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  delivery exe without source code py loky62 2 320 Apr-04-2024, 05:47 PM
Last Post: loky62
  Algorithm for extracting comments from Python source code Pavel1982 6 512 Feb-28-2024, 09:52 PM
Last Post: Pavel1982
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 7 6,664 Aug-03-2023, 06:00 PM
Last Post: Gribouillis
  difficult string conversion need help Pir8Radio 3 1,051 Nov-27-2022, 06:15 PM
Last Post: Gribouillis
  No Module found in other directory than source code [SOLVED] AlphaInc 1 2,041 Nov-10-2021, 04:34 PM
Last Post: AlphaInc
  Generating classes diagram from source code Pavel_47 3 5,612 Oct-01-2021, 03:31 AM
Last Post: Lou
  How do I open the Source code of a library? JaneTan 1 2,262 Aug-18-2021, 02:12 AM
Last Post: Larz60+
  Compiling Python 3.8.5 source code results in build error Deepan 0 2,178 Sep-14-2020, 04:11 AM
Last Post: Deepan
  C to Python code conversion print problem anakk1n 1 2,175 May-22-2020, 04:15 PM
Last Post: deanhystad
  If Python 2.x source code and applies a transform it into valid Python 3.x code. vivekm 1 2,159 May-20-2019, 05:12 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020