【问题标题】:Exporting and Importing a array from python从 python 导出和导入数组
【发布时间】:2015-06-08 15:13:22
【问题描述】:

我目前有下面的数组,我正在尝试将其导出到 .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"},
]

【问题讨论】:

标签: python arrays python-3.x export


【解决方案1】:

有很多方法可以保存数据,“正确”的方法实际上取决于上下文、用例等。但是,您的数据格式(字典列表)和您提到的文本文件强烈建议使用 @ 987654321@ 格式。 Python 使用 standardlib's csv module 让事情变得简单。

【讨论】:

  • 我喜欢你的回答,因为它不仅仅是给某人一个答案,而是让他们有机会自己做!特别是因为有很多途径可以做到这一点......并且已经有一些方法可以向他们展示。
  • @JohnRuddell 如果这不是赠品答案,他们会否决您的链接答案。人们过于依赖 Stack Overflow 程序,无法为自己采取行动。
  • @MalikBrahimi 这不是链接答案。 OP 没有指定他想使用哪种方法,并且显然没有对此进行任何研究,因为简单的谷歌搜索将为他提供解决方案。这就是为什么我喜欢这个答案并且它得到了支持。如果有人有实际问题并显示尝试,那么是的,代码解决方案就是要走的路。如果没有尝试,只是“我需要你为我做这件事”,那么他们应该为答案而努力。布鲁诺的回答为 OP 自己提供了必要的信息。除非他自己做,否则他不会学习。没有讲义! :)
【解决方案2】:

看起来您需要一个简单的保存到文本文件的案例。 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'>

【讨论】:

  • 我将如何使用类似这样的数据对数据进行排序 data = (sorted(data, key=operator.itemgetter("score"), reverse=True))
【解决方案3】:

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

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)

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

    • 我没有投反对票.. 但这可能是因为您的回答与在您最近一次编辑之前插入 .txt 文件(这是 OP 的要求)无关
    • @JohnRuddell NoSQL 数据库与 JSON 没有什么不同。您可以存储数据。
    • 我知道 :) 但我的意思是要求写入一个不存储在数据库中的.txt 文件。这可能是反对票的来源:/
    猜你喜欢
    • 1970-01-01
    • 2011-06-10
    • 2012-07-13
    • 2022-11-12
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多