擅长:python、mysql、java
<p>首先,不清楚为什么要在文件中出现<code>r"\n"</code>(两个字节),而不是{<cd2>}(一个字节)。输出文件的使用者要做什么?在每个输入字段上使用<code>ast.evaluate_literal()</code>?如果您的实际数据包含任何(非ASCII字符、撇号、引号),那么我会非常谨慎地使用<code>repr()</code>序列化它。在</p>
<p>第二,您要么误报了代码,要么误报了输出(或者两者都有)。您显示的代码实际上会生成:</p>
<pre><code>"I like to
ride my bike",pumpkin sauce
chicken,wings
</code></pre>
<p>第三,关于你的<code>"I like to \n ride my bike".encode("utf-8")</code>:<code>str_object.encode("utf-8")</code>是绝对没有意义的如果<code>str_object</code>只包含ASCII字节,它什么也不做。否则会引发异常。在</p>
<p>第四,这一评论:</p>
<blockquote>
<p>I don't need to call encode anymore, now that I'm using the raw
string. There are a lot of unicode characters in the text that I am
using, so before I started using the raw string I was using encode so
that csv could read the unicode text</p>
</blockquote>
<p>没有任何意义,正如我所说,<code>"ascii string".encode('utf8')</code>是没有意义的。在</p>
<p>考虑向后退一步或者两步,解释一下你真正想做的事情:你的数据从哪里来,里面有什么,最重要的是,读取文件的进程要做什么?在</p>