使用Python将多个空格替换为逗号。

huangapple go评论65阅读模式
英文:

Replace multiple spaces with comma in Python

问题

I am reading switch output from file and would like to do something with values then. If I read lines from file I have my data in following format with multiple spaces:

['Eth1/1         interfaceName connected  trunk      full     100G     QSFP-40/100-SRBD', 'Eth1/2         interfaceName2 connected  trunk      full     100G     QSFP-40/100-SRBD']

And I would need to delete those spaces and replace them with a comma. So the output should be like when you read lines from a .csv file:

['Eth1/1,interfaceName,connected,trunk,full,100G,QSFP-40/100-SRBD', 'Eth1/2,interfaceName2,connected,trunk,full,100G,QSFP-40/100-SRBD']

Any idea how to do it in a simple way?

I have tried to iterate over each character in the list and replace " " with "", but do not know how to enter one comma there.

英文:

I am reading switch output from file and would like to do something with values then.

If I read lines from file I have my data in following format with multiple spaces:

['Eth1/1         interfaceName connected  trunk      full     100G     QSFP-40/100-SRBD', 'Eth1/2         interfaceName2 connected  trunk      full     100G     QSFP-40/100-SRBD']

And I would need to delete those spaces and replace them with comma. So the output should be like when you read lines from .csv file:

['Eth1/1,interfaceName,connected,trunk,full,100G,QSFP-40/100-SRBD', 'Eth1/2,interfaceName2,connected,trunk,full,100G,QSFP-40/100-SRBD']

Any idea how to do it in simple way?

I have tried to iterate over each character in list and replace " " with "", but do not know how to enter one comma there.

答案1

得分: 4

我会使用.split(),因为它会根据一个或多个空白字符进行分割,然后按以下方式使用.join

text = "Able    Baker Charlie"
cleaned = ','.join(text.split())
print(cleaned)

输出结果为:

Able,Baker,Charlie
英文:

I would use .split() as it does split on one-or-more whitespace characters, then .join following way

text = "Able    Baker Charlie"
cleaned = ','.join(text.split())
print(cleaned)

gives output

Able,Baker,Charlie

答案2

得分: 1

使用内置的 re 模块:

import re

print(re.sub('s+', ',', your_string))
英文:

Use the built-in re module:

import re

print(re.sub('\s+', ',', your_string))

答案3

得分: 0

这是一个反对科技发展的回答:是的,反复向字符串追加会很慢,但这不是一个反对科技发展的回答。

def spaces2comma(l0, prev_is_comma=False, l1=""):
    for c in l0:
        if c == " ":
            if prev_is_comma:
                continue
            else:
                c = ","
                prev_is_comma = 1
        else:
            prev_is_comma = False
        l1 += c
    return l1

lines = ['Eth1/1    interfaceName    connected  trunk  full  100G  QSFP-40/100-SRBD',
         'Eth1/2    interfaceSurname connected  trunk  full  100G  QSFP-40/100-SRBD']
print(*(spaces2comma(l) for l in lines), sep='\n')

我忘了提到:

spaces2comma("    asde fgh   tyu", True)

输出结果为:'asde,fgh,tyu'

英文:

This is a Luddite answer: yes, repeatedly appending to a string is SLOOOW but, isn't it a Luddite answer?

In [60]: def spaces2comma(l0, prev_is_comma=False, l1=""):
    ...:     for c in l0:
    ...:         if c==" ":
    ...:             if prev_is_comma: continue
    ...:             else:c=",";prev_is_comma=1
    ...:         else:prev_is_comma=False
    ...:         l1+=c
    ...:     return l1
    ...: 
    ...: lines = ['Eth1/1    interfaceName    connected  trunk  full  100G  QSFP-40/100-SRBD',
    ...:          'Eth1/2    interfaceSurname connected  trunk  full  100G  QSFP-40/100-SRBD']
    ...: print(*(spaces2comma(l) for l in lines), sep='\n')
Eth1/1,interfaceName,connected,trunk,full,100G,QSFP-40/100-SRBD
Eth1/2,interfaceSurname,connected,trunk,full,100G,QSFP-40/100-SRBD

In [61]: 

I forgot to mention:

In [61]: spaces2comma("    asde fgh   tyu", True)
Out[61]: 'asde,fgh,tyu'

答案4

得分: -1

First use split() function to get a list of strings.

my_string = "'Eth1/1         interfaceName connected  trunk      full     100G     QSFP-40/100-SRBD', 'Eth1/2         interfaceName2 connected  trunk      full     100G     QSFP-40/100-SRBD'"
str = my_string.split()

Then join them using .join and use a comma as a separator.

new_string = ",".join(str)
英文:

First use split() function to get a list of strings.

my_string = "'Eth1/1         interfaceName connected  trunk      full     100G     QSFP-40/100-SRBD', 'Eth1/2         interfaceName2 connected  trunk      full     100G     QSFP-40/100-SRBD'"
str = my_string.split()

Then join them using .join and use comma as seperator.

new_string = ",".join(str)

huangapple
  • 本文由 发表于 2023年3月15日 18:12:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743278.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定