![](/img/trans.png)
[英]How can I store each key-value pair of my python dictionary on a separate line in json file?
[英]How to store each line in a file in a separate array?
我有一個文件,其內容如下:
3 5 7 9 11 13 15 17
17 15 13 11 9 7 5 3
3 5 7 9 8 7 6 5
17 15 13 11 12 13 14 15
我想將它存儲在 4 個不同的數組中,每行一個數組,例如
arr1=[3 5 7 9 11 13 15 17]
arr2=[17 15 13 11 9 7 5 3]
arr3=[3 5 7 9 8 7 6 5], and
arr4=[17 15 13 11 12 13 14 15]
您可以嘗試使用 python 上下文管理器來讀取您的文件,然后分配變量
with open(file name, 'r') as f:
arr1,arr2,arr3,arr4= [x.replace('\n'.'') for x in f.readlines()]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.