代码之家  ›  专栏  ›  技术社区  ›  Fachrul Rozy Saputra R

如何在csv中生成下一行中的单词

  •  0
  • Fachrul Rozy Saputra R  · 技术社区  · 6 年前

    我有这样一个清单:

    ['tomorrow', 'space', 'Film']
    

    当我将其写入csv时,它会变成:

    tomorrow,space,Film
    

    当我将其写入csv时,我的期望如下:

    tommorrow
    space
    Film
    

    这是我的密码

    with open("Noun.csv", 'wb') as n:
            noun = csv.writer(n)
            noun.writerow(list_noun)
    

    我如何修复我的代码?

    3 回复  |  直到 6 年前
        1
  •  1
  •   Ajax1234    6 年前

    你可以用 csv.writerows :

    import csv
    s = ['tomorrow', 'space', 'Film']
    with open('filename.csv', 'w') as f:
      write = csv.writer(f)
      write.writerows([[i] for i in s])
    
        2
  •  0
  •   Praveen Myakala    6 年前
    import csv
    list_noun = ['tomorrow', 'space', 'Film']
    with open('noun.csv', 'w') as f:
      [csv.writer(f).writerow([word]) for word in list_noun]
    
        3
  •  0
  •   alichaudry    6 年前

    你不需要 csv 包裹试试这个:

    data = ['tomorrow', 'space', 'Film']
    with open('_temp.csv', 'w') as handle:
        handle.write("\n".join(data))