我有一个这样的csv:
16;SILCOMP1;1;;;;;A;;;;;;;GO_SLAVE_10;niente
32;SILCOMP1;1;;;;A;;;;;;;;GO_SLAVE_10;niente
64;SILCOMP1;1;;A;;;;;;;;;;GO_SLAVE_10;niente
128;SILCOMP1;1;A;;;;;;;;;;;GO_SLAVE_10;niente
;;;;;;;;;;;;;;;
3;SILCOMP1;2;;;;;;;;;;B;A;niente;niente
5;SILCOMP1;2;;;;;;;;B;;;A;niente;niente
基本上我想创建一个代码,自动识别行";;;;"128到3之间的那个,删除或跳过它。将csv的每个原始数据移动到data_array之后,我试着用
len(data_array[i]) == 0
但似乎即使生是由";"它不是空的。任何想法?
与文件data.csv
类似
16;SILCOMP1;1;;;;;A;;;;;;;GO_SLAVE_10;niente
32;SILCOMP1;1;;;;A;;;;;;;;GO_SLAVE_10;niente
64;SILCOMP1;1;;A;;;;;;;;;;GO_SLAVE_10;niente
128;SILCOMP1;1;A;;;;;;;;;;;GO_SLAVE_10;niente
;;;;;;;;;;;;;;;
3;SILCOMP1;2;;;;;;;;;;B;A;niente;niente
5;SILCOMP1;2;;;;;;;;B;;;A;niente;niente
import csv
with open("data.csv", "r") as file:
data = [
row for row in csv.reader(file, delimiter=";")
if any(item != "" for item in row)
]
产生以下data
:
[['16', 'SILCOMP1', '1', '', '', '', '', 'A', '', '', '', '', '', '', 'GO_SLAVE_10', 'niente']
['32', 'SILCOMP1', '1', '', '', '', 'A', '', '', '', '', '', '', '', 'GO_SLAVE_10', 'niente']
['64', 'SILCOMP1', '1', '', 'A', '', '', '', '', '', '', '', '', '', 'GO_SLAVE_10', 'niente']
['128', 'SILCOMP1', '1', 'A', '', '', '', '', '', '', '', '', '', '', 'GO_SLAVE_10', 'niente']
['3', 'SILCOMP1', '2', '', '', '', '', '', '', '', '', '', 'B', 'A', 'niente', 'niente']
['5', 'SILCOMP1', '2', '', '', '', '', '', '', '', 'B', '', '', 'A', 'niente', 'niente']]