LoginSignup
0
1

More than 3 years have passed since last update.

【Python】数字が入ったListを文字列結合して出力ファイルへ書き出す

Posted at

pickle とかありましたが、数字が入っているとバグるようなので原始的にやります。


data = [[1,2,3,4,5,6], [1,2,3,4,5,6]]

def c(data):
    outputfile = open("./output.txt", "w")
    for d in data:
        if(len(d) == 6):
            str_list = map(str, d)
            write_data = ",".join(str_list)+"\n"
            outputfile.write(write_data)
    outputfile.close()

c(data)

結果

output.txt
1,2,3,4,5,6
1,2,3,4,5,6

Point
- 一度使用したファイルのcloseを忘れずに
- mapで要素全てをStringに変換

0
1
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
0
1