简体   繁体   English

在具有特殊字符的python中拆分

[英]Split in python with character special

I split within a string traversing an array with values, this split must contain the following rule:我在一个遍历数组的字符串中拆分,这个拆分必须包含以下规则:

  • Split the string into two parts when there is a special character, and select the first part as a result;有特殊字符时将字符串分成两部分,结果选择第一部分;

SCRIPT脚本

array = [
'srv1 #s',
'srv2;192.168.9.1'
]

result = []
for x in array:

  outfinally = [line.split(';')[0] and line.split()[0] for line in x.splitlines() if line and line[0].isalpha()]

  for srv in outfinally:
      if srv != None:
          result.append(srv)

for i in result:
  print(i)

OUTPUT输出

srv1
srv2;192.168.9.1

DESIRED OUTPUT期望的输出

srv1
srv2

You can split twice with the two different separators instead:您可以使用两个不同的分隔符拆分两次:

result = [s.split()[0].split(';')[0] for s in array]

result becomes: result变成:

['srv1', 'srv2']

The problem is here: line.split(';')[0] and line.split()[0]问题在这里: line.split(';')[0] and line.split()[0]

Your second condition splits on whitespace.您的第二个条件在空白处拆分。 As a result, it'll always return the whitespace-split version unless there's a semicolon at the start of the input (in which case you get empty string).因此,除非输入开头有分号(在这种情况下您会得到空字符串),否则它将始终返回空格分割版本。

You probably want to chain the two splits instead:您可能希望将两个拆分链接起来:

line.split(';')[0].split()[0]


To see what the code in your question is doing, take a look at what your conditional expression does in a few different cases:要查看问题中的代码在做什么,请查看您的条件表达式在几种不同情况下的作用:

array = ['srv1 s', 'srv2;192.168.9.1', ';192.168.1.1', 'srv1;srv2 192.168.1.1']

>>> for item in array:
...   print("Original: {}\n\tSplit: {}".format(item, item.split(';')[0] and item.split()[0]))
...
Original: srv1 s
        Split: srv1 # split on whitespace
Original: srv2;192.168.9.1
        Split: srv2;192.168.9.1 # split on whitespace!
Original: ;192.168.1.1
        Split: # split on special char, returned empty which is falsey, returns empty str
Original: srv1;srv2 192.168.1.1
        Split: srv1;srv2 # split only on whitespace

This should split on any special charters and append the first part of the split to a new list:这应该根据任何特殊章程进行拆分,并将拆分的第一部分附加到新列表中:

array = [
'srv1 #s',
'srv2;192.168.9.1'
]

sep = (r'[`\-=~!@#$%^&*()_+\[\]{};\'\\:"|<,./<>?]')
rest = text.split(sep, 1)[0]
new_array =[]
for i in array:
    new_array.append(re.split(sep,i)[0])

Output:输出:

['srv1 ', 'srv2']

Change改变

outfinally = [line.split(';')[0] and line.split()[0] for line in x.splitlines() if line and line[0].isalpha()]

To

outfinally = [line.replace(';', ' ').split()[0] for line in x.splitlines() if line and line[0].isalpha()]

When you use and like that, it will always return the first result as long as the first result is truthy.当您使用and like 时,只要第一个结果为真,它就会始终返回第一个结果。 The split function returns the full string in a list when a match is not found.当未找到匹配项时,split 函数返回列表中的完整字符串。 Since it's returning something truthy, you'll never move on to the second condition (and if you use or like I first tried to do, you'll always move on to the second condition).由于它返回的是真实的东西,你永远不会进入第二个条件(如果你使用or像我第一次尝试做的那样,你总是会进入第二个条件)。 Instead of having 2 conditions, what you'll have to do is combine them into one.您需要做的是将它们合二为一,而不是有 2 个条件。 Something like line.replace(';', ' ').split()[0] or blhsing's solution is even better.line.replace(';', ' ').split()[0]或 blhsing 的解决方案甚至更好。

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

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