1

我有一个字符串如下

command= "copy1 "D:\path1\program Files" "c:\program Files\path1" copy2 "D:\path2\program Files" "c:\program Files\path2""

请注意,copy1 和 copy2 都在字符串内。我的意图是将每个复制过程写在一个 xml 文件中,如下所示:

        <copyFiles>
               <copy1>
                    <Sourcepath>D:\path1\program Files</Sourcepath>
                    <DestPath>c:\program Files\path1</DestPath>
                </copy1>

                 <copy2>
                    <Sourcepath>D:\path2\program Files</Sourcepath>
                    <DestPath>c:\program Files\path2</DestPath>
                  </copy2>

         <copyFiles>

我尝试通过使用空格拆分字符串来创建每个参数的列表,如下所示:

Copyparamlist=command.split(' ')

但我收到的 xml 如下:

        <copyFiles>
               <copy1>
                    <Sourcepath>D:\path1\program</Sourcepath>
                    <DestPath>c:\program</DestPath>
                </copy1>

                 <copy2>
                    <Sourcepath>D:\path2\program</Sourcepath>
                    <DestPath>c:\program</DestPath>
                  </copy2>

         <copyFiles>

我的清单内容应该如下: ["copy1", "D:\path1\program Files", "c:\program Files\path1", "copy2", "D:\path2\program Files]", "c:\program Files\path2"]

请帮忙

4

7 回答 7

1

很难说出你到底想要什么,但看起来你想按如下方式拆分字符串:

a = 'this "is a string" with "4 elements"'
split = magic_splitter(a)  #How to write magic splitter?
print split  #['this', 'is a string', 'with', '4 elements']

如果是这样的话,你想要的是使用梦幻般的功能shlex.split

print shlex.split(a) #['this', 'is a string', 'with', '4 elements']
于 2012-07-26T15:01:12.313 回答
0

你想要什么输出?您可以使用join它以您想要的任何格式放置它。例如,要简单地删除空格,请使用

Copyparamlist=''.join(command.split(' '))

或者,如果您想用 %20 替换它们,请使用

Copyparamlit='%20'.join(command.split(' '))
于 2012-07-26T14:56:05.343 回答
0

尝试以下示例:

import shlex
import xml.etree.ElementTree as etree

def my_parser(s):
    command = []
    for item in shlex.split(s):
        if item.startswith('copy'):
            if command:            # any previous command was collected?
                yield command      # output one parsed command
            command = [ item ]     # start collecting new command
        else:
            command.append(item)   # collect another argument    

    if command:                    # if anything was collected
        yield command              # output the last parsed command


cmd = r'copy1 "D:\path1\program Files" "c:\program Files\path1" copy2 "D:\path2\program Files" "c:\program Files\path2"'

# Just to show what are the parsed command in the lists
for command in my_parser(cmd):
    print command

# Alternatively, constructing the XML.
e = etree.Element('copyFiles')    # root element
tree = etree.ElementTree(e)
root = tree.getroot()             # now the same as e 

for command in my_parser(cmd):
    assert len(command) == 3
    assert command[0].startswith('copy')

    e = etree.Element(command[0])  # the command name becomes the tag name 

    src = etree.Element('Sourcepath')  # subelement
    src.text = command[1]

    dst =  etree.Element('DestPath')   # subelement
    dst.text = command[2]

    e.append(src)                  # subelement appended to its parent
    e.append(dst)                  # subelement appended to its parent

    root.append(e)                 # command appended to the root element

tree.write('output.xml')

它打印:

['copy1', 'D:\\path1\\program Files', 'c:\\program Files\\path1']
['copy2', 'D:\\path2\\program Files', 'c:\\program Files\\path2']

它产生output.xml以下内容(作为一行,在此处手动包装):

<copyFiles><copy1><Sourcepath>D:\path1\program Files</Sourcepath>
<DestPath>c:\program Files\path1</DestPath></copy1>
<copy2><Sourcepath>D:\path2\program Files</Sourcepath>
<DestPath>c:\program Files\path2</DestPath></copy2></copyFiles>
于 2012-07-26T16:26:32.493 回答
0

除了空格之外,您还需要有另一个分隔符...例如...您可以使用例如引号分隔符,例如

CopyParam = command.split('"')

我是否应该将字符串命令的格式理解为 copyName "Source" "Destination" ...您可以在三元组列表中循环...比如

for i in xrange(0, len(CopyParam), 3): 
    copyname, sourcepath, destpath = CopyParam[i], CopyParam[i + 1], CopyParam[i + 2]
    # Do the code to transfer it to XML here ..

您可能需要先从列表中删除空元素..

于 2012-07-26T15:01:49.160 回答
0

我假设您的命令字符串实际上指定为;

command='copy1 "something with spaces" "somewhere else" copy2 "this thing" "elsewhere"'

如果您的命令始终是复制命令,则可以查看拆分“复制”一词并使用结果列表,

command.split('copy')
['','1 "something with spaces" "somewhere else" ','2 "this thing" "elsewhere"']
于 2012-07-26T15:02:57.290 回答
0

我认为这个线程可能有你正在寻找的东西。如果您试图忽略引号中的拆分,您可能必须使用正则表达式。

如何在python中拆分但忽略引用字符串中的分隔符?

试试这个代码:

import re
PATTERN = re.compile(r'''((?:[^ "']|"[^"]*"|'[^']*')+)''')
command = PATTERN.split(command)
于 2012-07-26T15:12:47.957 回答
0

有了这个,它可以很容易地作为一个字典来管理:

import shlex
split = shlex.split(commandlist)
commands = dict(zip(split[::3], zip(split[1::3], split[2::3])))

解释:给定字符串 'copyname "source" "dest" copyname2 "source2" "dest2"'...

split = shlex.split() 将按照格式拆分字符串['copyname', 'source', 'dest', 'copyname2', 'source2','dest2']

split[::3]正在做一种叫做拼接的事情。这隐含地说“从 0 索引开始,每三项给​​我一次。split[::3]将等于['copyname', 'copyname2']。不过,拼接并不止于此。split[1::3]说从索引 1 开始,然后每隔三个元素给出。它将返回所有来源。

zip功能是独一无二的。给定列表l1=['a','b','c']l2=[1,2,3]zip(l1,l2)等于[('a',1),('b',2), ('c',3)]

zip(split[1::3], split[2::3])将等于[('source', 'dest'), ('source2', 'dest2')]

zip(split[::3], zip(split[1::3], split[2::3]))将等于[('copyname',('source','dest')), ('copyname2',('source2','dest2'))]

然后,该dict函数会将给定列表中的每个键/值对 ( copynameand ('source','dest')) 转换为字典。

commands变量现在以格式写入{'copyname': ('source', 'dest'), ...} xml,只需遍历字典即可。

专业提示:使用 遍历字典时for,每个项目都是一个键。

如果你愿意,你可以使用for copyname, source, dest in zip(split[::3], split[1::3], split[2::3]).

于 2012-07-26T15:13:08.683 回答