Writing a list of tuples to a text file in Python

后端 未结 6 735
执笔经年
执笔经年 2021-01-05 14:11

I have a list of tuples in the format:

(\"some string\", \"string symbol\", some number)

For example, (\"Apples\", \"=\", 10).

6条回答
  •  逝去的感伤
    2021-01-05 14:48

    This example will allow you to loop through a list of tuples and write each tuple to a new line in a text file. Note that im using unicode instead of string:

    with open('filename.txt','w') as f:
            for tup in list_of_tuples:
                f.write( u" ".join(map(unicode,tup))+u"\n")
    

    The above answers map your data to string format first, which is okay if your data is not already in unicode format.

提交回复
热议问题