无法保存映射到文本文件的两个列表 - python

Not able to save two lists mapped to a text file - python

目前,我正在尝试保存我已经能够映射到不同列表的号码列表。当我打印它时效果很好 :) - 但是当我尝试将它保存到文本文件时出现错误。

这是运行时的代码:

print (*map(numbers.__getitem__, names), sep=",")

但是,当我尝试将上面的结果保存到 txt 文件时,我收到一个错误:invalid syntax - 不知道为什么。

这是将其保存到 txt file:

的代码
file = open("contacts.txt","w")  
file.write(*map(numbers.__getitem__, names), sep=",")
file.close() 

如果有人能在这里帮助我,我会很高兴 - 我不明白我如何能够打印到终端但无法将最终结果保存到文本文件

@wim,我尝试了下面的代码:

with file.open("contacts.txt","w") as f: 
    print(*map(numbers.__getitem__, names), sep=",", file=f)
    file.close() 

就是说file没有声明!不起作用

print 接受多个参数。 file.write 没有。

试试这个:

with open('contacts.txt', 'w') as f:
    print(*map(numbers.__getitem__, names), sep=",", file=f)

或者,使用 csv 模块。