小编Kim*_*uey的帖子

如何让Python的ElementTree打印到XML文件?

背景

我正在使用SQLite访问数据库并检索所需的信息.我在Python 2.6版中使用ElementTree来创建包含该信息的XML文件.

import sqlite3
import xml.etree.ElementTree as ET

# NOTE: Omitted code where I acccess the database,
# pull data, and add elements to the tree

tree = ET.ElementTree(root)

# Pretty printing to Python shell for testing purposes
from xml.dom import minidom
print minidom.parseString(ET.tostring(root)).toprettyxml(indent = "   ")

#######  Here lies my problem  #######
tree.write("New_Database.xml")
Run Code Online (Sandbox Code Playgroud)

尝试

我已经尝试使用tree.write("New_Database.xml", "utf-8")上面代码的最后一行,但它根本没有编辑XML的布局 - 它仍然是混乱的混乱.

我还决定摆弄并尝试做:而不是将其打印到Python shell,这给出了错误AttributeError:'unicode'对象没有属性'write'.
tree = minidom.parseString(ET.tostring(root)).toprettyxml(indent = " ")

问题

当我将我的树写到最后一行的XML文件时,是否有一种方法可以像在Python shell中那样打印到XML文件?

我可以toprettyxml()在这里使用,还是有不同的方法来做到这一点?

python xml pretty-print elementtree python-2.6

30
推荐指数
6
解决办法
5万
查看次数

C++错误:将'char*'赋值给'char [2]的类型不兼容

我的构造函数有点问题.在我的头文件中,我声明:

char short_name_[2]; 
Run Code Online (Sandbox Code Playgroud)
  • 和其他变量

在我的构造函数中:

Territory(std::string name, char short_name[2], Player* owner, char units);
void setShortName(char* short_name);
inline const char (&getShortName() const)[2] { return short_name_; }
Run Code Online (Sandbox Code Playgroud)

在我的cpp文件中:

Territory::Territory(std::string name, char short_name[2], Player* owner, 
                     char units) : name_(name), short_name_(short_name), 
                    owner_(owner), units_(units)
{ }
Run Code Online (Sandbox Code Playgroud)

我的错误:

Territory.cpp:在构造函数'Territory :: Territory(std :: string,char*,Player*,char)'中:Territory.cpp:15:33:错误:将'char*'赋值给'char [的不兼容类型2]"

我已经想通了,char[2] <=> char*但我不知道如何处理关于我的构造函数和get/setter的问题.

c++ error-handling char

9
推荐指数
1
解决办法
5万
查看次数

struct没有名为的成员

我的程序包含一个包含两个数组成员的结构.我已经将结构称为函数参数中的void函数.

结构定义:

struct caketime
{
    double baking_time [4]={20,75,40,30};
    double prepare_time[4]={30,40,25,60};
};
Run Code Online (Sandbox Code Playgroud)

虚函数:

void prepareorder(struct caketime p) {

int i=0;
    for (i=0;i<LIMIT;i++)
    {
        if(p.prepare_time[i]==25)
            printf("Choclate");
        else if (p.prepare_time[i]==30)
            printf("Sponge Cake");
        else if (p.prepare_time[i]==45)
            printf("Meringue");
        else if (p.baking_time[i]==60)
            printf("Red_velvet");
    }
}
Run Code Online (Sandbox Code Playgroud)

当我编译这个程序时,我得到下面描述的错误:

In function 'prepareorder': error: 'struct caketime' has no member named 'prepare_time'
error: 'struct caketime' has no member named 'baking_time'
Run Code Online (Sandbox Code Playgroud)

这里似乎有什么问题?

c arrays function data-structures

7
推荐指数
1
解决办法
2万
查看次数

如何使用 Python 的 ElementTree 在属性名称中转义冒号?

背景

我在 Python 2.6 版中使用 ElementTree 创建一个 XML 文件(使用从数据库中检索到的数据)。

代码

以下代码行是问题区域,因为由于属性名称中的冒号,我不断收到语法错误。

# Please ignore any errors the "^" characters would cause if they were
# actually part of my code - just using them as placeholders.

root = ET.Element("databaseConfiguration", xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance",
                                                ^
                  xsi:noNamespaceSchemaLocation="database.xsd")
                     ^
Run Code Online (Sandbox Code Playgroud)

转义这些属性名称中的冒号以root等效于以下内容的最有效方法是什么:

<databaseConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="database.xsd"/>

笔记

我查看了 Stack Overflow 上的一些解决方案(例如solution1solution2solution3solution4),其中用户正在解析 XML 文件,但我似乎无法将这些修复程序解释为适用于写入 XML 的修复程序。



提前致谢!

python xml escaping elementtree python-2.6

5
推荐指数
1
解决办法
2358
查看次数

如何使用Perl从文件中读取多行值

我有一个属性文件,比方说

##
## Start of property1
##
##
Property1=\
a:b,\
a1:b1,\
a2,b2
##
## Start of propert2
##
Property2=\
c:d,\
c1:d1,\
c2,d2
Run Code Online (Sandbox Code Playgroud)

请注意,任何给定属性的值可以分为多行.

我想用Perl读取这个属性文件.这在Java中运行良好,因为Java使用反斜杠支持多行值,但在Perl中它是一个噩梦.

在上面的属性文件中有两个属性 - Property1Property2- 每个属性与一个字符串相关联,我可以根据分隔符,:

对于给定的属性(比如说Property1)和给定的列(比如说a1)我需要返回第二列(这里b1)

代码应该能够忽略注释,空格等.

提前致谢

perl

3
推荐指数
1
解决办法
2699
查看次数

jquery if else语句不起作用

我正在检查变量wal,我从PHP文件中获取该变量json_encode.如果wal值小于100,则应在其中呈现日历selectable = false.否则,它应该呈现日历selectable = true.

我的代码看起来像这样(fullCalendar不包含在此代码段中):

$(document).ready(function () {
    if (wal < "100") {
        alert (wal);
        var calendar = $('#calendar').fullCalendar(
        {
            selectable: false,
        }
    });

    } else {
        alert (wal); 
        var calendar = $('#calendar').fullCalendar(
        {
            selectable: true,}
        });   
    };
}
Run Code Online (Sandbox Code Playgroud)

在一种情况下,war= 120,所以它应该触发else条件,但它仍然呈现selectable = false.

我的错误在哪里?

编辑1

我换 if (wal < 100)if (parseInt(wal) < 100).使用wal= 120,它会跳转到else语句中.但是,如果我改为 …

javascript php jquery fullcalendar

2
推荐指数
1
解决办法
90
查看次数