从字典字典中读取特定键值(Reading specific key values from a dictionary of dictionary)
我有一本字典词典如下:
dictofdict = {'001': {'date': 201504, 'status':'New'}, '002': {'date':201504, 'status':'Constant', 'rate':'-10'}, '003':{'date': 201504, 'status':'In Review'}}
内部词典可能有也可能没有以下所有键:日期,状态和速率。 例如:001和003在其值字典中没有键速率。
我如何阅读这本字典词典,以便我有如下输出。 我只对内部词典中的日期和状态键感兴趣。
201504 001 New 201504 002 Constant 201504 003 In Review
I have a dictionary of dictionaries as follows:
dictofdict = {'001': {'date': 201504, 'status':'New'}, '002': {'date':201504, 'status':'Constant', 'rate':'-10'}, '003':{'date': 201504, 'status':'In Review'}}
The inner dictionaries may or may not have all the following keys: date, status and rate. For example: 001 and 003 do not have the key rate in their dictionary of values.
How do I read this dictionary of dictionaries such that I have an output as follows. I am interested in only the date and status keys from the inner dictionaries.
201504 001 New 201504 002 Constant 201504 003 In Review
原文:https://stackoverflow.com/questions/30739405
最满意答案
Dockerfile
每个RUN
指令都在不同的层中执行(如RUN
文档中所述)。您的
Dockerfile
有三条RUN
指令。 问题是MySQL服务器只能在第一个启动。 在其他的,没有MySQL正在运行,这就是为什么你的mysql
客户端连接错误。要解决这个问题,你有2个解决方案。
解决方案1:使用单行
RUN
RUN /bin/bash -c "/usr/bin/mysqld_safe &" && \ sleep 5 && \ mysql -u root -e "CREATE DATABASE mydb" && \ mysql -u root mydb < /tmp/dump.sql
解决方案2:使用脚本
创建可执行脚本
init_db.sh
:#!/bin/bash /usr/bin/mysqld_safe & sleep 5 mysql -u root -e "CREATE DATABASE mydb" mysql -u root mydb < /tmp/dump.sql
将这些行添加到
Dockerfile
:ADD init_db.sh /tmp/init_db.sh RUN /tmp/init_db.sh
Each
RUN
instruction in aDockerfile
is executed in a different layer (as explained in the documentation ofRUN
).In your
Dockerfile
, you have threeRUN
instructions. The problem is that MySQL server is only started in the first. In the others, no MySQL are running, that is why you get your connection error withmysql
client.To solve this problem you have 2 solutions.
Solution 1: use a one-line
RUN
RUN /bin/bash -c "/usr/bin/mysqld_safe --skip-grant-tables &" && \ sleep 5 && \ mysql -u root -e "CREATE DATABASE mydb" && \ mysql -u root mydb < /tmp/dump.sql
Solution 2: use a script
Create an executable script
init_db.sh
:#!/bin/bash /usr/bin/mysqld_safe --skip-grant-tables & sleep 5 mysql -u root -e "CREATE DATABASE mydb" mysql -u root mydb < /tmp/dump.sql
Add these lines to your
Dockerfile
:ADD init_db.sh /tmp/init_db.sh RUN /tmp/init_db.sh
相关问答
更多-
如果您可以再次创建转储,则可以在转储时将--insert-ignore添加到命令行。 或者您可以尝试使用带有--force的mysqlimport命令,即使遇到MySQL错误,该命令也会继续。 If you can make the dump again you could add --insert-ignore to the command-line when dumping. Or you can try using the mysqlimport command with --force,which ...
-
有什么数据库专业书籍介绍?[2022-05-19]
mysql -
Dockerfile每个RUN指令都在不同的层中执行(如RUN文档中所述)。 您的Dockerfile有三条RUN指令。 问题是MySQL服务器只能在第一个启动。 在其他的,没有MySQL正在运行,这就是为什么你的mysql客户端连接错误。 要解决这个问题,你有2个解决方案。 解决方案1:使用单行RUN RUN /bin/bash -c "/usr/bin/mysqld_safe &" && \ sleep 5 && \ mysql -u root -e "CREATE DATABASE mydb" ...
-
打开sql文件并注释掉尝试创建现有数据库的行。 Open the sql file and comment out the line that tries to create the existing database.
-
如果你看一下官方的Docker MySQL图像Dockerfile ,你会发现他们是如何使用debconf-set-selections做的 。 相关说明是: RUN { \ echo mysql-community-server mysql-community-server/data-dir select ''; \ echo mysql-community-server mysql-community-server/root-pass password ''; \ ...
-
好像我的数据库出了问题。 重新创建它解决了这个问题。 It seems that there was a problem with my database. Recreating it solved that problem.
-
不,你可能不会。 Oracle导出是一种专有的,未记录的文件格式。 实际上,它只能由Oracle导入实用程序读取。 可能有一些第三方工具可能反向设计了文件格式,但我不知道任何会导入MySQL的。 如果将数据导入本地Oracle数据库,则将其传输到MySQL数据库变得更加容易。 任何ETL工具都可以这样做,你可以编写一个应用程序来移动数据,你可以从Oracle连接到MySQL来推送数据,你可以从Oracle数据库中提取DDL和DML等。 No, you probably can't. An Oracle ex ...
-
导入mysql转储(Importing mysql dump)[2024-01-03]
尝试这样的事情: ... $dbhost = "blaa.mysql.eu1.frbit.com"; $dbuser = "dbusername"; $dbpass = 'mypasswordhere'; $dbname = "database_name"; $filename = "/var/www/blaa/sqlfilehere.sql" system("mysql -h $dbhost -u $dbuser --password='$dbpass' $dbname < $filename"); . ... -
问题是你从未启动数据库 - 你需要在大多数Docker镜像中显式启动服务。 但是如果你想在Docker(DB和你的python程序)中运行两个进程,事情会变得复杂一些。 您要么必须使用像主管这样的流程管理器,要么在启动脚本中更聪明一些。 要查看我的意思,请创建以下脚本,并将其cmd.sh : #!/bin/bash mysqld & python main.py 将其添加到Dockerfile: FROM ubuntu:saucy # Install required packages RUN apt ...
-
在两个系统上,您必须检查您的连接编码是否正确: SHOW VARIABLES LIKE 'character_set_%' 通常看到这样的字符是双重编码的结果。 确保您可以将连接和客户端编码匹配完全相同。 有许多命令行选项可以促进这一点,或者如果您正在使用驱动程序或客户端,那里的东西可以调整它。 On both systems you must check that your connection encoding is correct: SHOW VARIABLES LIKE 'character_se ...