How to write list of strings to file, adding newlines?

前端 未结 9 1378
借酒劲吻你
借酒劲吻你 2020-12-31 09:02
def generator():
    nums = [\'09\', \'98\', \'87\', \'76\', \'65\', \'54\', \'43\']
    s_chars = [\'*\', \'&\', \'^\', \'%\', \'$\', \'#\', \'@\',]

    data =         


        
9条回答
  •  情话喂你
    2020-12-31 09:51

    As other answers gave already pointed out, you can do it by appending a '\n' to c+n or by using the format string "%s%s\n".

    Just as a matter of interest, I think it would be more pythonic to use a list comprehension instead of two nested loops:

    data.write("\n".join("%s%s"%(c,n) for c in s_chars for n in nums))
    

提交回复
热议问题