从python导出和导入数组

时间:2015-06-08 15:13:23

标签: python arrays python-3.x export

我目前有下面的数组,我正在尝试将其导出到.txt文件,然后将其导回到python中,我将如何进行此操作。

data = [
    {"score": "10", "grade": "E", "music": "song5", "maxcombo": "1", "perfect": "20", "great": "1", "good": "20", "miss": "1"},
    {"score": "20", "grade": "D", "music": "song4", "maxcombo": "2", "perfect": "20", "great": "2", "good": "20", "miss": "2"},
    {"score": "30", "grade": "C", "music": "song3", "maxcombo": "3", "perfect": "20", "great": "3", "good": "20", "miss": "3"},
    {"score": "40", "grade": "B", "music": "song2", "maxcombo": "4", "perfect": "20", "great": "4", "good": "20", "miss": "4"},
    {"score": "50", "grade": "A", "music": "song1", "maxcombo": "5", "perfect": "20", "great": "5", "good": "20", "miss": "5"},
]

4 个答案:

答案 0 :(得分:3)

有许多方法可以保存数据,而“正确”的方法实际上取决于上下文,用例等。但是,您的数据格式(一系列文字)和您提及的文本文件强烈建议使用{ {3}}格式。使用csv

可以轻松实现Python

答案 1 :(得分:0)

这会将您的数据写入输出文件并帮助您入门。那么你所要做的就是阅读它,这很容易。

import json

data = [
{"score": "10", "grade": "E", "music": "song5", "maxcombo": "1", "perfect": "20", "great": "1", "good": "20", "miss": "1"},
{"score": "20", "grade": "D", "music": "song4", "maxcombo": "2", "perfect": "20", "great": "2", "good": "20", "miss": "2"},
{"score": "30", "grade": "C", "music": "song3", "maxcombo": "3", "perfect": "20", "great": "3", "good": "20", "miss": "3"},
{"score": "40", "grade": "B", "music": "song2", "maxcombo": "4", "perfect": "20", "great": "4", "good": "20", "miss": "4"},
{"score": "50", "grade": "A", "music": "song1", "maxcombo": "5", "perfect": 
"20", "great": "5", "good": "20", "miss": "5"},
]

outputfile = 'output.json'# create this file
with open(outputfile, 'wb') as outfile:
    json.dump(row, outfile)

答案 2 :(得分:0)

看起来您需要一个保存到文本文件的简单案例。 json可能很容易开始。

试试这个: 1.将内容json保存到文本文件中 2.您可以再次加载数据并转换为json,这将像dict对象一样工作。

file_path = '/tmp/dict_test.txt'
import json
data = [
    {"score": "10", "grade": "E", "music": "song5", "maxcombo": "1", "perfect": "20", "great": "1", "good": "20", "miss": "1"},
    {"score": "20", "grade": "D", "music": "song4", "maxcombo": "2", "perfect": "20", "great": "2", "good": "20", "miss": "2"},
    {"score": "30", "grade": "C", "music": "song3", "maxcombo": "3", "perfect": "20", "great": "3", "good": "20", "miss": "3"},
    {"score": "40", "grade": "B", "music": "song2", "maxcombo": "4", "perfect": "20", "great": "4", "good": "20", "miss": "4"},
    {"score": "50", "grade": "A", "music": "song1", "maxcombo": "5", "perfect": "20", "great": "5", "good": "20", "miss": "5"},
]

# dump the dict contents using json 
with open(file_path, 'w') as outfile:
    json.dump(data, outfile, indent=4, separators=(',', ':'))

# Let's read the data back again from the file
file_text = ''
with open(file_path, 'rt') as file_placeholder:
    lines = file_placeholder.readlines()
    file_text = ''.join(lines)  # This provides the whole file data as string

print('file_text = {}'.format(file_text))
# Load as json
json_text = json.loads(file_text)
print('json = {}'.format(json_text))
print('file_text type = {}; json_text type = {}'.format(type(file_text), type(json_text)))

您将获得以下结果:

file_text = [
    {
        "good":"20",
        "grade":"E",
        "great":"1",
.............
    }
]
json = [{'good': '20', 'grade': 'E', 'great': '1', 'music': 'song5', 'score': '10', 'miss': '1', 'maxcombo': ................ 'perfect': '20'}]
file_text type = <class 'str'>; json_text type = <class 'list'>

答案 3 :(得分:-1)

您可以pickle文件中的数据,它会将data列表序列化为Python对象:

pickle.dump(data, open('data.pkl', 'w')) # dump the contents of data into a file