简体   繁体   English

如何在保持 \\n 的同时拆分字符串

[英]How to split string while keeping \n

I want to write the first letter of every item while linebreak stays the same but when I turn the list to string it's written in one line.我想写每个项目的第一个字母,而换行符保持不变,但是当我将列表转换为字符串时,它写在一行中。 Like this "I wtwfloe I wlss" but I want output to look like this "I wt \\nwtfl \\noei \\nwl \\nss" .像这样"I wtwfloe I wlss"但我希望输出看起来像这样"I wt \\nwtfl \\noei \\nwl \\nss"

r = '''I want to
write the first letter 
of every item
while linebreak
stay same'''

list_of_words = r.split()
m = [x[0] for x in list_of_words]
string = ' '.join([str(item) for item in m])
print(string)

What you are doing is you are splitting all the lines in a single go, so you are losing the information of each line.您正在做的是一次性拆分所有行,因此您正在丢失每行的信息。 You need to create list of list to preserve the line information.您需要创建列表列表以保留线路信息。

When you provide no argument means split according to any whitespace , that means both ' ' and '\\n' .当您不提供no argument means split according to any whitespace ,这意味着' ' and '\\n'

r = '''I want to
write the first letter 
of every item
while linebreak
stay same'''

list_of_words = [i.split() for i in r.split('\n')]
m = [[y[0] for y in x] for x in list_of_words]
string = '\n'.join([' '.join(x) for x in m])
print(string)
I w t
w t f l
o e i
w l
s s

Via regexp通过正则表达式

r = '''I want to
write the first letter 
of every item
while linebreak
stay same'''

import re

string = re.sub(r"(.)\S+(\s)", r"\1\2", r + " ")[:-1]

print(string)

Output:输出:

I t
w t f l 
o e i
w l
s s

What you're doing is - Get the first letter from each word of the list and then joining them.您正在做的是 -从列表中的每个单词中获取第一个字母,然后加入它们。 You are not keeping track of the \\n in the string.您没有跟踪字符串中的\\n

You could do this instead.你可以这样做。

list_of_words = r.split('\n')
m = [[x[0] for x in y.split()] for y in list_of_words]
for i in m:
    string = ' '.join(i)
    print(string)
Output

I w t
w t f l
o e i
w l
s s

Here is the solution by using while loop这是使用while循环的解决方案

r = '''I want to
write the first letter 
of every item
while linebreak
stay same'''


total_lines = len(r.splitlines())
line_no = 0
while line_no < total_lines:
    words_line = r.splitlines()[line_no]
    list_of_words = words_line.split()
    m = [x[0] for x in list_of_words]
    print(' '.join([str(item) for item in m]))
    line_no = line_no + 1

Since many valid methods were already provided, here's a nice and comprehensive way of doing the same task without the use of str.split() , which creates unnecessary list intermediates in memory (not that it represents any problem in this case though).由于已经提供了许多有效的方法,这里有一个很好且全面的方法来完成相同的任务,而不使用str.split() ,这会在内存中创建不必要的列表中间体(但在这种情况下它并不代表任何问题)。

This method takes advantage of str.isspace() to deliver the whole set of instructions in one line:此方法利用str.isspace()在一行中提供整套指令:

string = "".join([string[i] for i in range(len(string)) if string[i].isspace() or string[i-1].isspace() or i == 0])

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

相关问题 如何拆分字符串并保持模式 - How to split a string and keeping the pattern 如何在保持空白的同时拆分? - How to split while keeping the empty line? 如何在保留 alignment 的同时拆分并行语料库? - How to split parallel corpora while keeping alignment? 如何通过没有空格的 substring 分割字符串,同时保留其原始空格? - How to split a string by a substring without white spaces, while keeping its original white spaces? Python 3 - 如何将字符串中的每个字符拆分为列表,同时保持十进制数字不变? - Python 3 - How to split every character in a string into a list while keeping decimal numbers intact? 使用多个分隔符在python中拆分字符串的最佳方法 - 同时保留分隔符 - Best way to split a string in python with multiple separators - while keeping the separators python正则表达式拆分字符串,同时保持分隔符的值 - python regex split string while keeping delimiter with value Python 在保持分隔符的同时将字符串拆分/切片到列表中 - Python Split/Slice a string into a list while keeping delimeter 如何将字符串转换为列表,按字符串&#39;\\ n \\ n \\ n \\ n \\ n&#39;拆分? - How to convert a string to a list, split by the string '\n\n\n\n\n'? 在特定子字符串上拆分字符串并保留它 - Split String on specific substring and keeping it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM