I get messages from /dev/ttyAMA0 using this code:

 @app.route('/rmem_memory', methods=['POST']) def _rmem_memory(): if 'username' in session: cmmd = 'h\r' port.write(cmmd.encode('ascii')) output = port.read(871) d = collections.OrderedDict() d['status'] = 200 d['output'] = output return flask_json.dumps(d, sort_keys=False, indent=True) return 'nothing' 

I get in response JSON in this form:

 { "status": 200, "output": "h\r\nI2C-SPI-Tools usage:\r\nv display version of I2C-SPI-Tools\r\nh print this help\r\nx reset peripheral ICs\r\nt print timing\r\nI2C bus:\r\nl list all slave devices\r\nr [ [number of bytes to read]]\r\n read up to 256 data bytes from specified slave device\r\nw [data byte] [...] [data byte]\r\n write up to 256 data byte to specified slave device\r\n all values can be integer or hexadecimal\r\n indicate hexadecimal values with leading \"0x\"\r\n hexadecimal values are proceeded by \"0x\"\r\n state the 7-bit version of the slave address\r\n values in square brackets are optional\r\n de toggle DE function\r\nSPI bus:\r\n rw \r\n mcu define MCU as SPI Master\r\n ext define external source as SPI Master\r\n am analyzer mode\r\ntoggle echo?\t^E\r\n>" } 

I would like to format it correctly, remove \r\n and everything in a new line.

  • Inside JSON lines, hyphenation is prohibited by standard. - andreymal

1 answer 1

 d['output'] = output.replace('\r\n', '\n') 
  • It looks doubtful ... - Qwertiy
  • does not work at all, replaces \ r \ n with \ n but does not format in after \ n into a new line - Insider
  • This is because I could not understand the phrases "and everything in the new line" and "does not format in after \ n in the new line", no matter how hard I tried. The \n character is the new line. - Sergey Gornostaev
  • @SergeyGornostaev is my fault, yes. I did not know how to explain. In general, the output via minicom prntscr.com/cc0h1q , in this format would be desirable in JSON, but it seems like \ n JSON is unknown - Insider
  • Ahhh, so you want to break the line in the json file with hyphenation? This is a format violation, it will not be read later. For these purposes, the control sequence \n serves just as well, it is well known to json parsers. - Sergey Gornostaev