1
我有表,看起來像這樣:如何用Python中的0替換文本表中的空白條目?
text = """
ID = 1234
Hello World 135,343 117,668 81,228
Another line of text (30,632) (48,063)
More text 0 11,205 0
Even more text 1,447 681
ID = 18372
Another table 35,323 38,302 909,381
Another line with text 13 15
More text here 7 0
Even more text here 7,011 1,447 681
"""
有沒有辦法用0來代替每個表中的「空白」項?我想在條目之間設置分隔符,而是使用下面的代碼不能與表中的空白點處理:
for line in text.splitlines():
if 'ID' not in line:
line1 = line.split()
line = '|'.join((' '.join(line1[:-3]), '|'.join(line1[-3:])))
print line
else:
print line
輸出是:
ID = 1234
|
Hello World|135,343|117,668|81,228
Another line of|text|(30,632)|(48,063)
More text|0|11,205|0
Even more|text|1,447|681
|
ID = 18372
|
Another table|35,323|38,302|909,381
Another line with|text|13|15
More text|here|7|0
Even more text here|7,011|1,447|681
正如你所看到的,第一個問題出現在第一個表的第二行。 「文本」這個詞被認爲是第一列。任何方式來解決這個在Python中用0代替空白條目?
這將工作我上面列出的表。但是,每個表的列位置可能不同,並且有數千個表。這可以修改,以避免手動檢查每個表的列的開始位置嗎? – myname
表2的列可能沒有像上面的示例那樣排在表1的列的正下方。每個表格只有3列。 – myname
非常感謝你! – myname