简体   繁体   English

从文件中读取字典

[英]Read dictionary from file

Background (optional) 背景(可选)

I am writting a python script to analyse Abaqus (finite element software) outputs. 我正在编写一个python脚本来分析Abaqus(有限元软件)的输出。 This software generates a ".odb" which has a propriedtary format. 该软件生成具有专有格式的“ .odb”。 However you can access the data stored inside of the databse thanks to python libraries specialy developped by Dassault (owner of the Abaqus sofware). 但是,由于Dassault(Abaqus软件的所有者)专门开发了python库,因此您可以访问存储在数据库内部的数据。 The python script has to be run by the software in order to access these libraries : python脚本必须由软件运行才能访问以下库:

abaqus python myScript.py

However it is really hard to use new libraries this way, and I cannot make it run the matplotlib library. 但是,以这种方式使用新库确实很困难,而且我无法使其运行matplotlib库。 So I would like to export the array I created inside a file, and access it later with an other script that would not require to be run using abaqus 因此,我想导出在文件中创建的数组,并稍后使用不需要使用abaqus运行的其他脚本来访问它。

The Problem 问题

In order to manipulate the data, I am using collections. 为了操纵数据,我使用了集合。 For exemple: 举个例子:

coord2s11=defaultdict(list)

This array stores the Z coordinate of a group of nodes and their stress value, at each time step: 此数组在每个时间步存储一组节点的Z坐标及其应力值:

coord2s11[time_step][node_number][0]=z_coordinate
coord2s11[time_step][node_number][1]=stress_value

For a given time step, the output would be : 对于给定的时间步长,输出为:

defaultdict(<type 'list'>, {52101: [-61.83229635920749, 0.31428813934326172], 52102: [-51.948098314163417, 0.31094224750995636],[...], 52152: [440.18335942363655, -0.11255115270614624]})

And the glob (for all step time): 以及(所有步骤时间)的全局变量:

defaultdict(<type 'list'>, {0.0: defaultdict(<type 'list'>, {52101: [0.0, 0.0],[...]}), 12.660835266113281: defaultdict(<type 'list'>, {52101: [0.0, 0.0],[...],52152: [497.74876378582229, -0.24295337498188019]})})

If it is visually unpleasant, it is rather easy to use ! 如果视觉上不愉快,则非常易于使用! I printed this array inside this file using: 我使用以下命令将此数组打印在此文件中:

with open('node2coord.dat','w') as f :
    f.write(str(glob))

I tried to follow the solution I found on this post , but when I try to read the file a store the value inside a new dictionnay 我试图遵循在这篇文章中找到的解决方案,但是当我尝试读取文件时,将值存储在新的字典中

import ast

with open('node2coord.dat', 'r') as f:
    s = f.read()
    node2coord = ast.literal_eval(s)

I end up with a SyntaxError: invalid syntax , that I guess comes from the defaultdict(<type 'list'> here and there in the array. 我最终遇到了SyntaxError: invalid syntax ,我猜想是来自defaultdict(<type 'list'>在数组中到处都有。

Is there a way to get the data stored inside of the file or should I modify the way it is written inside the file ? 有没有办法获取存储在文件中的数据,还是应该修改写入文件中的数据的方式? Ideally I would like to create the exact same array I stored. 理想情况下,我想创建与我存储的完全相同的数组。

The solution by Joel Johnson 乔尔·约翰逊(Joel Johnson)的解决方案

Creating a database using shelve. 使用货架创建数据库。 It is an easy and fast method. 这是一种简便快捷的方法。 The following code did the trick for me to create the db : 以下代码帮助我创建了db:

import os
import shelve

curdir = os.path.dirname(__file__) #defining current directory

d = shelve.open(os.path.join(curdir, 'nameOfTheDataBase')) #creation of the db
d['keyLabel'] = glob # storing the dictionary in "d" under the key 'keyLabel'
d.close() # close the db

The "with" statement did not work for me. “ with”语句对我不起作用。 And then to open it again : 然后再次打开它:

import os
import shelve


curdir = os.path.dirname(__file__)
d = shelve.open(os.path.join(curdir, 'nameOfTheDataBase')) #opening the db
newDictionary = d['keyLabel'] #loading the dictionary inside of newDictionary
d.close()

If you ever get an error saying 如果您遇到错误提示

ImportError: No module named gdbm

Just install the gdbm module. 只需安装gdbm模块。 For linux : 对于Linux:

sudo apt-get install python-gdbm

More information here 更多信息在这里

If you have access to shelve (which I think you would because it's part of the standard library) I would highly recommend using that. 如果您可以使用书架 (我认为您会这样做 ,因为它是标准库的一部分),我强烈建议您使用它。 Using shelve is an easy way to store and load python objects without manually parsing and reconstructing them. 使用搁架是一种存储和加载python对象的简便方法,而无需手动解析和重建它们。

import shelve

with shelve.open('myData') as s:
    s["glob"] = glob

Thats it for storing the data. 多数民众赞成在存储数据。 Then when you need to retrieve it... 然后,当您需要检索它时...

import shelve

with shelve.open('myData') as s:
   glob = s["glob"]

It's as simple as that. 就这么简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM