python write to file from dictionary

后端 未结 2 1948
半阙折子戏
半阙折子戏 2020-12-11 10:59

I\'m new with Python and I know that piece of code is very simple and lacks some statements, actually I need to write to file from dictionary. This code runs but only writes

2条回答
  •  盖世英雄少女心
    2020-12-11 11:42

    If you want to keep adding lines to your file, open it with the a mode, as described in the documentation:

    for (name, mobile) in ab.iteritems():
        with open(...., "a") as f:
            print ('Contact %s at %s' % (name, mobile))
            f.write(name)
            f.write(mobile)
    

    Using w as mode means writing: your file will be overwritten.

提交回复
热议问题