python 3中的字符串拆分格式

时间:2013-06-20 19:37:03

标签: python formatting newline

我正在尝试格式化此字符串,其中一行包含五个单词。但是,我一直把它作为输出:

  

我爱饼干是的我会看到一只狗

首先,我不是在一行中得到5个单词,而是在一行中得到所有内容。

其次,为什么“让我们”分裂?我想在使用“单词”拆分字符串时,只有在它们之间有空格时才会拆分?

连连呢?

string = """I love cookies. yes I do. Let's see a dog."""


# split string
words = re.split('\W+',string)

words = [i for i in words if i != '']


counter = 0
output=''
for i in words:
    if counter == 0:
        output +="{0:>15s}".format(i)

# if counter == 5, new row
    elif counter % 5 == 0:
       output += '\n'
       output += "{0:>15s}".format(i)

    else:
       output += "{0:>15s}".format(i)

    # Increase the counter by 1
    counter += 1

print(output)

2 个答案:

答案 0 :(得分:20)

首先,不要调用变量“string”,因为它会使module同名阴影

其次,使用split()进行分词

>>> s = """I love cookies. yes I do. Let's see a dog."""
>>> s.split()
['I', 'love', 'cookies.', 'yes', 'I', 'do.', "Let's", 'see', 'a', 'dog.']

来自re-module

  
    
      

\ W       匹配任何非Unicode字符的字符。这与\ w相反。如果使用ASCII标志,则它变为等效于[^ a-zA-Z0-9_](但是该标志影响整个正则表达式,因此在这种情况下使用显式[^ a-zA-Z0-9_]可能是更好的选择)。

    
  

由于'未在上面列出,因此使用的正则表达式将“Let's”字符串拆分为两部分:

>>> words = re.split('\W+', s)
>>> words
['I', 'love', 'cookies', 'yes', 'I', 'do', 'Let', 's', 'see', 'a', 'dog', '']

这是我使用strip() - 上面的方法获得的输出:

$ ./sp3.py 
              I           love       cookies.            yes              I
            do.          Let's            see              a           dog.

counter==0以来,代码可能简化为此,而else子句也做同样的事情。我也可以通过enumerate来摆脱柜台:

#!/usr/bin/env python3

s = """I love cookies. yes I do. Let's see a dog."""
words = s.split()

output = ''
for n, i in enumerate(words):
    if n % 5 == 0:
        output += '\n'
    output += "{0:>15s}".format(i)
print(output)

答案 1 :(得分:1)

words = string.split()
while (len(words))
     for word in words[:5]
          print(word, end=" ")
     print()
     words = words[5:]

这是基本概念,使用split()方法将其拆分

然后使用切片表示法对其进行切片以获得前5个单词

然后切掉前5个单词,然后再循环

相关问题