[英](python): split after each dot into a new line and capitalize each first letter of each line with keeping the dots
[英]split line and keep first few columns with each new line
我试图在每次出现逗号时创建新行,但将前几列保留在新行中。 为了清楚起见:
A B C, D,
E F Z
G H I, J,
最后应该看起来像:
A B C
A B D
E F Z
G H I
G H J
我是python的新手,但是我正在尝试,所以我知道split对我来说是最好的,但是我不知道如何保留前几列(实际上要保留7列),并且对于逗号的每种外观,请创建前7列仍在的新行
到目前为止,我尝试了:
lines = df.split(",")
for column in lines:
print(column)
这会给我:
A B C
D
E F Z
G H I
J
我知道的不多,但这只是一个开始:)
import copy
example = 'A\tB\tC,\tD,\nE\tF\tZ\nG\tH\tI,\tJ,'
lines = example.split('\n')
new_text = ''
for line in lines:
line = line.rstrip(',').split('\t')
new_lines = [[]]
index = 0
for element in line:
if ',' in element:
new_lines.append(copy.deepcopy(new_lines[index]))
new_lines[index].append(element.rstrip(','))
index += 1
else:
new_lines[index].append(element)
for new_line in new_lines:
new_text += '\t'.join(new_line)+'\n'
print(new_text)
这给
A B C
A B D
E F Z
G H I
G H J
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.