首页 \ 问答 \ scala , python哪个用来开发spark更好

scala , python哪个用来开发spark更好

准备研究spark了,发现支持的语言有 java,scala,python 其中,scala,python都不熟悉,准备学其中一门用来开发spark。java就算了,麻烦 请问,实际中,哪个用的多,哪个更适合开发spark
更新时间:2023-01-20 08:01

最满意答案

如果唯一的要求是application之前的permission ,则可以通过在包含Element位置0插入permissions (使用insert方法)来确保。

import StringIO
from xml.etree import ElementTree

xml_str = "<root><something></something><application></application></root>"
permission_str = "<permission></permission>"

tree = ElementTree.parse(StringIO.StringIO(xml_str))
perm_element = ElementTree.fromstring(permission_str)

# insert our permission element as the first child
tree.getroot().insert(0, perm_element)

print ElementTree.tostring(tree.getroot())

收益率:

<root><permission /><something /><application /></root>

编辑

如果您需要更精细的控制,您有几个选择。 可能最好的选择是安装和使用lxml ,它具有insertBefore功能。

如果您更喜欢坚持xml.etree ,这里有一个更精确的插入新元素的方法:

import StringIO
from xml.etree import ElementTree


def get_index(root, element):
    for idx, child in enumerate(root):
        if element == child:
            return idx
    else:
        raise ValueError("No '%s' tag found in '%s' children" %
                         (element.tag, root.tag))

xml_str = """<root><something></something><application></application></root>"""
tree = ElementTree.parse(StringIO.StringIO(xml_str))

# print our tree pre-insertion
print ElementTree.tostring(tree.getroot())

permission_str = "<permission></permission>"
perm_element = ElementTree.fromstring(permission_str)
app_element = tree.getroot().find('./application')
app_index = get_index(tree.getroot(), app_element)
tree.getroot().insert(app_index, perm_element)

# print our tree post-insertion
print ElementTree.tostring(tree.getroot())

在运行时打印:

<root><something /><application /></root>
<root><something /><permission /><application /></root>

这有一些限制 - 如果有几个相同的元素,它会在找到的第一个元素之前插入新元素(因此,如果你有两个具有相同属性的root应用程序子元素,并希望在第一个元素之后但在第二个,你运气不好。 也就是说,我相信它能完成你所需要的。


If the only requirement is that permission be before application, you can ensure that by inserting permissions at position 0 of the containing Element (using the insert method).

import StringIO
from xml.etree import ElementTree

xml_str = "<root><something></something><application></application></root>"
permission_str = "<permission></permission>"

tree = ElementTree.parse(StringIO.StringIO(xml_str))
perm_element = ElementTree.fromstring(permission_str)

# insert our permission element as the first child
tree.getroot().insert(0, perm_element)

print ElementTree.tostring(tree.getroot())

yields:

<root><permission /><something /><application /></root>

EDIT:

You have a couple of options if you need finer grained control. Probably the best option is to install and use lxml, which does have an insertBefore capability.

If you prefer to stick to xml.etree, here's a more precise method of inserting new elements:

import StringIO
from xml.etree import ElementTree


def get_index(root, element):
    for idx, child in enumerate(root):
        if element == child:
            return idx
    else:
        raise ValueError("No '%s' tag found in '%s' children" %
                         (element.tag, root.tag))

xml_str = """<root><something></something><application></application></root>"""
tree = ElementTree.parse(StringIO.StringIO(xml_str))

# print our tree pre-insertion
print ElementTree.tostring(tree.getroot())

permission_str = "<permission></permission>"
perm_element = ElementTree.fromstring(permission_str)
app_element = tree.getroot().find('./application')
app_index = get_index(tree.getroot(), app_element)
tree.getroot().insert(app_index, perm_element)

# print our tree post-insertion
print ElementTree.tostring(tree.getroot())

which, when run, prints:

<root><something /><application /></root>
<root><something /><permission /><application /></root>

This has limitations - if there are several identical elements, it will insert the new element before the first one it finds (so if you had two application children of root with identical attributes and wanted to place a new tag after the first one but before the second one, you'd be out of luck). That said, I believe it accomplishes what you need it to.

相关问答

更多
  • 如果唯一的要求是application之前的permission ,则可以通过在包含Element位置0插入permissions (使用insert方法)来确保。 import StringIO from xml.etree import ElementTree xml_str = "" permission_str = "" ...
  • 如果您使用xml.etree.ElementTree.parse从文件中解析,则可以使用xml.etree.ElementTree.fromstring从文本中解析。 请参阅xml.etree.ElementTree If you're using xml.etree.ElementTree.parse to parse from a file, then you can use xml.etree.ElementTree.fromstring to parse from text. See xml.etr ...
  • 没有直接支持parent属性的形式,但您可以使用此处描述的模式来实现所需的效果。 建议以下一行(从链接到帖子)为整个树创建一个子对象映射: parent_map = dict((c, p) for p in tree.getiterator() for c in p) There's no direct support in the form of a parent attribute, but you can perhaps use the patterns described here to achi ...
  • 是的,这是按预期工作的。 我的理解是,如果标签附加了一个名称空间,那么所有子元素也应该在同一个名称空间中。 实际上,如果标记声明了默认名称空间,那么所有子元素也将位于同一名称空间中。 你的EntityContainer元素是这样的: 这会创建一个新的命名空间,但不会将EntityContainer及其子项放在其中。 为了满足您的期望,您必须声明一个默认命名空间,如下所示:
  • 正如文档所说,在write方法中: 文件是一个文件名,或者是一个为写入而打开的文件对象。 这包括一个StringIO对象。 所以: outfile = cStringIO.StringIO() tree.write(of) 然后你可以使用你最喜欢的方法漂亮地打印outfile - 只需outfile.seek(0)然后将outfile本身传递给一个接受文件的函数,或者将outfile.getvalue()传递给一个接受字符串的函数。 但是请注意,在链接的问题中很多打印XML的方法甚至不需要这些。 例如: ...
  • ElementTree使用XPath子集来选择XML树中的节点。 您可以使用tree.findall( './/bar' )查找树中的所有bar节点。 ElementTree uses an XPath subset for selecting nodes within the XML tree. You can use tree.findall( './/bar' ) to find all bar nodes within the tree.
  • More text和Some more text是some-tag尾巴。 请参阅以下内容: >>> import xml.etree.cElementTree as et >>> text = """ text1 text2 text3 More text Some more text Even more text """ >>> root = et.fromstr ...
  • 您遇到了问题14246中最近记录的错误。 在修复之前,Python 3的一个解决方法是将sys.stdin更改为byte流而不是string流: import sys import xml.etree.cElementTree as ET sys.stdin = sys.stdin.detach() tree = ET.parse(sys.stdin) You've run into the bug recently documented in Issue 14246. Until it is fixe ...
  • 在你的情况下,你应该用.iter()替换.getiterator() ,你可能应该调用它作为root元素,而不是树(但我不确定,因为我没有Python 2.4和模块在我手中)。 import elementtree.ElementTree as ET tree = ET.parse('XML_file.xml') root = tree.getroot() for elem in root.getiterator(): print elem.tag, elem.attrib 这是Python 2. ...
  • 您在文档中引用的iter与__iter__方法不同。 要以深度优先顺序迭代所有标记,请使用如下的iter方法: >>> for e in t.iter(): print(e) ... 相比之下, __iter__只会迭代直接的孩子。 The iter tha ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)