On Feb 12, 2020, at 06:11, allu.matikainen@hotmail.com wrote:
I would like if the DictWriter would be able to write escape characters (\n and \r and their combinations such as \n\n\n and \r\n) as literal characters.
I don’t think you’re asking to write escape sequences, but how to write actual newline and carriage return characters by escaping them. Thats what the strings in your example post text have, at least. In a string literal, "a\nb" is the three characters a, newline, b; the string literal "a\\nb" is the four character a, backslash, n, b In your comments, you seem to have a lot of confusion about the difference between Python string literals, JSON string encodings, and the underlying strings, so it’s hard to be sure, but I’m about 90% sure that your actual strings have newlines, not backslash-escaped newlines. But regardless of what you actually have, there’s no reason the csv module should be changed to help you with this. It’s meant to write data in the same language used by Excel (or some other known CSV dialect). There is no backslash escaping of control characters in Excel—as is obvious from the fact that Excel shows a \n as literally a backslash and an n rather than a newline within the cell. If there’s some other CSV dialect that does use backslash escaping that you want to support, that would be different—but you’re trying to use Excel, so that can’t be the issue. The csv module doesn’t help you do web percent encoding or rot13 encryption or reversing every other string because those are meaningless to Excel and other CSV dialects, and the same is true here. But that’s fine. You can already arbitrarily transform strings in Python, before passing them to the csv module. For example, instead of `c.writerow(row)` you can do `c.writerow(map(transformer, row))`, and you’re done. That transformer could be an existing escaping function that means exactly what you want, or you could write it yourself as a one-liner (`return value.replace('\r', '\\r').replace('\n', '\\n')`). Also, even if you disagree and think the csv module does need to change, you need to explain what that change is. Would you add a new Dialect attribute? What would it be? Would it share the same escapechar used for escaping quotes, or have a different attribute, or be hardcoded to backslash? And so on.
I asked a question about this on Stack Overflow but have had no good answers yet.
You got comments telling you to use str.replace to modify the strings as you read them or as you write them. Which is the right answer. Your reply was that you don’t have permission to modify the text. So you’re asking for is a way to modify the text without modifying the text, which is obviously impossible. If that’s true, then even if Python 3.9 added your requested feature and you waited until it came out before continuing your project, you still wouldn’t be able to use it, because if you don’t have permission to replace newlines with \n escapes then you don’t have permission to ask the csv module to do it either. So no wonder you haven’t gotten any good answers. If you asked how to encode some text in UTF-16 and then said you don’t have permission to encode the text, you’d get the same result—either no answers, or bad answers from people who don’t know anything but are desperate for points so they just guess wildly at something that might be kind of similar to what you want.