So what you need to do is to prevent Python from escaping characters. (As for the line continuation character, it simply takes the newline out.) See the answer from According to the question, it looks like you attempted to escape line breaks in Python two ways: by using the line continuation character ( "\") or by using "\n" as an escape.īut keep in mind: if you are using a string in python, special escaped characters ( "\t", "\n") are translated into REAL control characters! The "\n" will be replaced with the ASCII control character representing a newline character, which is precisely the character that is illegal in JSON. JSON does not allow "real" newlines in its data it can only have escaped newlines. This is a really old question, but I came across this on a search and I think I know the source of your problem. Otherwise, this is one of the numerous ways that json isn't designed for human-readability. IF you can change the data format, then you can substitute an array of strings. Short of devising a pre-processor of some kind (and I, for one, don't feel like effectively making up my own language to solve this issue), there isn't a general solution to this problem. Nor does its (fairly small) grammar include any facility for representing a string on multiple lines. You can't tell it to concatenate strings. In many programming languages, even if they don't explicitly support splitting strings across lines, you can still use string concatenation to get the desired effect and as long as the compiler isn't awful this is fine.īut json is not a programming language it's just a data representation. (And even the answers that recognize this provide "solutions" that assume one is free to change the data representation, which in many cases one is not.)Īnd the worse news is, there is no good answer. The question is how to make the code look nicer by splitting the string value across multiple lines of code. Unfortunately many of the answers here address the question of how to put a newline character in the string data.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |