简体   繁体   中英

Printing a literal python string in octal

For reasons unknown I am the consumer of metadata that needs to be displayed in octal format to be used. I am trying to figure out how to represent it as an octal. Example:

x = "\n)\n\022foobar"
print x 

)
foobar

print repr(x)
'\n)\n\x12foobar'

how do i get x to print the way it was assigned, which is in octal encoding?

You can't; that representation is lost. If you want to output "\\022" then you need to either process it yourself or you need to store "\\\\022" instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM