简体   繁体   English

将字符串拆分为列表,但引号中的两个单词为一个?

[英]Split string into list, but two words in quotation mark as one?

I'm trying to make my own little console in Python3 and I'm trying to split my given commands.我正在尝试在 Python3 中制作自己的小控制台,并且正在尝试拆分给定的命令。

For example:例如:

mkdir dir becomes arg[0] = mkdir, arg[1] = dir mkdir 目录变为 arg[0] = mkdir, arg[1] = dir

I know i can do that with args.split(' '), but I'm trying to make it so that anything in quotation becomes one argument.我知道我可以用 args.split(' ') 做到这一点,但我正在努力做到这一点,以便引用中的任何内容都成为一个论点。

For example:例如:

mkdir "New Folder" becomes arg[0] = mkdir, arg[1] = New Folder. mkdir "新文件夹" 变为 arg[0] = mkdir, arg[1] = 新文件夹。

You can do the following using shlex :您可以使用shlex执行以下操作:

import shlex
res = shlex.split('mkdir "New Folder"')
print(res)
# ['mkdir', 'New Folder']

Another option using re :使用re的另一个选项:

import re
[p for p in re.split("( |\\\".*?\\\"|'.*?')", 'mkdir "New Folder"') if p.strip()]
# ['mkdir', '"New Folder"']

Or:或者:

import re
res3 = re.findall("(?:\".*?\"|\S)+", 'mkdir "New Folder"')
print(res3)
# ['mkdir', '"New Folder"']

Other option using csv :使用csv其他选项:

import csv
res4 = list(csv.reader(['mkdir "New Folder"'], delimiter=' '))[0]
print(res4)

This should work:这应该有效:

def splitArgsIntoArguments(args):
    result = args.split(' ')
    i = 0
    length = len(result)
    while i <  length:
        if result[i][0] == '"':
            # Trim string
            result[i] = result[i][1:]
            # Then for each next element merge it with result[i] until it ends with '"'
            for j in range(i+1 , len(result)):
                stop = False
                if result[j][-1] == '"':
                    # Trim string
                    result[j] = result[j][0:-1]
                    stop = True
                result[i] += " " + result[j]
                # Remove the jth element from the list
                result = result[0:j] + result[j+1:]
                # Then substract one from the length to not get out of range error
                length -= 1
                if stop: break
        i += 1

    return result

It's a bit ugly, but the function goes through each argument in the result list and if it starts with '"' it merges all elements to the right of it until it finds one that ends with '"'.这有点难看,但是 function 遍历结果列表中的每个参数,如果它以 '"' 开头,它将合并其右侧的所有元素,直到找到以 '"' 结尾的元素。

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

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