从 python 导出和导入数组
Exporting and Importing a array from python
我目前有下面的数组,我正在尝试将其导出到 .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"},
]
您可以 pickle
文件中的数据,这会将 data
列表序列化为 Python 对象:
pickle.dump(data, open('data.pkl', 'w')) # dump the contents of data into a file
这会将您的数据写入输出文件并帮助您入门。然后你只需要阅读它,这很容易。
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)
保存数据的方法有很多种,"right" 一种确实取决于上下文、用例等。但是,您的数据格式(字典列表)和您对文本文件的提及强烈建议使用 csv format. Python makes it easy with the standardlib's csv module.
看来您需要一个保存到文本文件的简单案例。 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'>
我目前有下面的数组,我正在尝试将其导出到 .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"},
]
您可以 pickle
文件中的数据,这会将 data
列表序列化为 Python 对象:
pickle.dump(data, open('data.pkl', 'w')) # dump the contents of data into a file
这会将您的数据写入输出文件并帮助您入门。然后你只需要阅读它,这很容易。
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)
保存数据的方法有很多种,"right" 一种确实取决于上下文、用例等。但是,您的数据格式(字典列表)和您对文本文件的提及强烈建议使用 csv format. Python makes it easy with the standardlib's csv module.
看来您需要一个保存到文本文件的简单案例。 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'>