将整个列表元素添加到文件中

时间:2014-02-20 14:58:35

标签: python python-2.7

我的Python脚本存在以下问题。我有一个包含一堆lat长坐标的列表,如此...

l = [('-12.3790,45.2880',), ('-11.4660,43.8350',), ('-10.2810,42.1890',),   ('-9.0860,40.7450',), ('-7.8840,39.4580',), ('-6.6660,38.3070',), ('-5.4330,37.2710',)]

我希望将该列表写入文件,以便数据在文本文件中显示如下:

l = [('-12.3790,45.2880'), ('-11.4660,43.8350'), ('-10.2810,42.1890'),   ('-9.0860,40.7450'), ('-7.8840,39.4580'), ('-6.6660,38.3070'), ('-5.4330,37.2710')]

请注意,我不想在lat()之后的逗号后面使用逗号。我怎么能这样做?

当我这样做时:

with open('file.txt', 'a') as f:
    f.write(l[:])

这不起作用

1 个答案:

答案 0 :(得分:1)

假设您确实想要问题中的输出格式,可以这样做:

"l = [" + ", ".join(["('" + t[0] + "')" for t in l]) + "]"