python 格式化单个值以在符合RFC4180的CSV文件中使用。使用逗号将这个函数返回的多个值连接到crea

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 格式化单个值以在符合RFC4180的CSV文件中使用。使用逗号将这个函数返回的多个值连接到crea相关的知识,希望对你有一定的参考价值。

def _format_csv_value(self, value):
    """Takes a single CSV value string and formats it in compliance with RFC4180.
    Multiple values can be joined together by putting them in a list and using ",".join(the_list).
    http://tools.ietf.org/html/rfc4180#page-3
    
    :param value: A single value destined to be output among multiple in a CSV row
    
    :return: The escaped and/or quoted string if necessary, otherwise simply returns <value>.
    """
    for x in [",", '"', "\n", "\r\n"]:
        if x in value:
            # Must replace double quotes '"' with two double quotes '""'
            value = value.replace('"', '""')
            # and contain all fields in double quotes if they contain commas or double quotes
            value = '"%s"' % value
            break
    return value

以上是关于python 格式化单个值以在符合RFC4180的CSV文件中使用。使用逗号将这个函数返回的多个值连接到crea的主要内容,如果未能解决你的问题,请参考以下文章