![](/img/trans.png)
[英](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.