Using python csv writer without quotations

前端 未结 4 925
猫巷女王i
猫巷女王i 2020-12-31 11:47

I\'m trying to write a list of strings like below to a file separated by the given delimiter.

res = [u\'123\', u\'hello world\']

When I try

4条回答
  •  耶瑟儿~
    2020-12-31 12:46

    You can set the csv.writer to quote nothing with quoting=csv.QUOTE_NONE for example:

    import csv
    with open('eggs.csv', 'wb') as csvfile:
        spamwriter = csv.writer(csvfile, delimiter=' ',
                                escapechar=' ', quoting=csv.QUOTE_NONE)
        spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
        spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
    

    Produces:

    Spam Spam Spam Spam Spam Baked  Beans
    Spam Lovely  Spam Wonderful  Spam
    

    If you do QUOTING_NONE you also need and escape character.

提交回复
热议问题