1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Python]辞書を1行ずつファイルに書き込む

Posted at

keyとvalueの組を1行ずつファイル出力するプログラム
パッケージのimportなどは不要

dic = dict(key1="val1", key2="val2", key3="val3")

# ファイル出力
with open("output.csv", mode='w') as f:
    f.writelines("\n".join(str(k)+","+str(v) for k,v in dic.items()))
# key1,val1
# key2,val2
# key3,val3

注意点
joinの中に二重リストや二重タプルを入れることはできません.
また異なる型をjoinで結合しようとするとエラーを吐くため,結合前に各変数をstr型に変換してあげないといけません.

1
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?