How to store or read a literal carriage return and newline from yaml in python -



How to store or read a literal carriage return and newline from yaml in python -

i have been struggling problem day. couldn't seem find reply online either. have yaml document stores server config message/response server , 1 of parameters "message_terminator". can guess, server knows message terminator of messages sent clients.

\r\n

is default sent telnet, have set such.

yaml document:

global: server_port: 7040 bound_ip: 0.0.0.0 message_terminator: \r\n

what want either read message_terminator value actual carriage homecoming , newline or convert string representation binary escape codes: homecoming , newline, not string representation "\r\n"

for instance, if in python:

print('\r\n')

it prints carriage homecoming , newline, not characters. if read value yaml config in python with:

print(config['global']['message_terminator'])

it prints out characters:

\r\n

changing yaml documents , adding quotes like:

global: server_port: 7040 bound_ip: 0.0.0.0 message_terminator: '\r\n'

and

global: server_port: 7040 bound_ip: 0.0.0.0 message_terminator: b'\r\n'

or even

global: server_port: 7040 bound_ip: 0.0.0.0 message_terminator: !!str \r\n

or

global: server_port: 7040 bound_ip: 0.0.0.0 message_terminator: !!python/bytes b'\r\n'

has no effect. when message terminator printed out prints characters. when sent on network clients, characters.

i have tried things like:

print(bytes(config['global']['message_terminator'], 'utf-8').encode('unicode_escape'))

which still prints out characters.

please excuse me if have totally wrong or missing big. i'm still working out whole difference between strings, bytes, raw strings, etc. advice or pointers welcome. time.

edit @jan: when in in interpreter works. carriage , newline printed. not characters.

>>> text = """ ... val: "hello\\n\\rnew line" ... """ >>> text '\nval: "hello\\n\\rnew line"\n' >>> print(text) val: "hello\n\rnew line" >>> import yaml >>> info = yaml.load(text) >>> info {'val': 'hello\n\rnew line'} >>> print(data['val']) hello new line >>>

but in main code, when load yaml file prints \r\n not actual carriage homecoming , newline. python code:

term = config['global']['message_terminator'] print(term) print(config['global']['message_terminator']) print("netcmd server started on port", port)

prints in terminal:

$ python3 netcmd.py \r\n \r\n netcmd server started on port 7040

to clarify, want action of carriage homecoming , newline, not there character representation.

edit 2: problem solved. alter yaml file after jan's update from:

global: server_port: 7040 bound_ip: 0.0.0.0 message_terminator: "\\r\\n"

to

global: server_port: 7040 bound_ip: 0.0.0.0 message_terminator: "\r\n"

everything's working , python code prints out carriage homecoming , newline read yaml file.

yaml allows escaping using \ character, have set value quotes:

>>> text = """ ... val: "hello\\n\\rnew line" ... """ ... >>> text '\nval: "hello\\n\\rnew line"\n' >>> info = yaml.load(text) >>> info {'val': 'hello\n\rnew line'}

so file global work follows:

global: server_port: 7040 bound_ip: 0.0.0.0 message_terminator: "\r\n"

section 5.7 of yaml spec 1.2:

5.7. escaped characters

all non-printable characters must escaped. yaml escape sequences utilize “\” notation mutual modern computer languages. each escape sequence must parsed appropriate unicode character. original escape sequence presentation detail , must not used convey content information.

note escape sequences interpreted in double-quoted scalars. in other scalar styles, “\” character has no special meaning , non-printable characters not available.

python yaml

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -