用于循环检查没有空值的单元格并附加

时间:2017-11-15 05:28:16

标签: python excel csv

这里有新的python编码器。

我想摆脱数据行并创建一个新的数据列表,其中任何空(或null)的单元格都不会被附加到新列表中。

这是我的代码:

chippedlist=[]
for line in stringlist:         #for each row of data in datafile
    chippedline=[]
    inc=0
    for cell in line:               #for each cell in one row of data      
        inc+=1
    if inc == 13 and len(cell)== 0:
        chippedline=[]
        break
    else:
        if cell == cell[0]:
            firstcell=cell[1:]
            chippedline.append(firstcell)
        if cell != cell[0] and cell != cell[-1]:
            chippedcell=cell[2:-1]
            chippedline.append(chippedcell)
        if cell == cell[-1]:
            lastcell=cell[:0]
            chippedline.append(lastcell)
if len(chippedline) > 0:
    chippedlist.append(chippedline)

我在这篇文章中附上了我的数据样本。

如您所见,从第一行开始,列'N'处有一个空单元格(在excel文件中),这相当于我创建'inc == 13'的增量(因为索引开始)我在python中创建了一个增量。

因此,如果任何行中的任何单元格为空(例如,行15和18,列Z和列AA),我不希望将该行包含到我的新数据集中。但是,我的for循环中的第一个if语句不起作用。

但是,它仍会将具有空单元格的行附加到新数据集中,如下面的输出所示:

['17040200555', 'A', 'A60', 'LATEMADA60', 'EMAD ', 'LAT', 'KL', 'KL/E', 'SAI', 'HK', 'HK/E', 'E', 'EMA', '', 'SWH1', 'BHL0', '580', 'PYNEH', '2017-04-02 08:29:10', '2017-04-02 08:29:10', '2017-04-02 08:29:16', '2017-04-02 08:29:16', '2017-04-02 08:29:39', '2017-04-02 08:29:40', '2017-04-02 08:40:53', '2017-04-02 08:57:38', '2017-04-02 09:10:19', '2017-04-02 09:24:33', '1', '703', '3293', '1005', '0', 'N', 'N', 'Y', 'Y', 'Y', 'Y', '', '', '0', '600', '2017-04-01 00:00:00', 'LAT', 'N-OTHERS', 'X600-ROSTER not foun', 'ssign to default 083', '030', '201704', '0800-0829', 'JC20170410P1.5.0', "2017-04-27 16:09:53'"]

['17040200554', 'A', 'A195', 'WONEMADA195', 'EMAD ', 'WON', 'KL', 'KL/E', 'LAI', 'KL', 'KL/W', 'E', 'EMA', '¥þ¨\\xad¥X¯l', 'LCK2', 'CHE0', '185', 'CMC', '2017-04-02 08:28:38', '2017-04-02 08:28:38', '2017-04-02 08:28:48', '2017-04-02 08:28:48', '2017-04-02 08:29:04', '2017-04-02 08:29:05', '2017-04-02 08:31:35', '2017-04-02 08:39:00', '2017-04-02 08:43:17', '2017-04-02 08:57:36', '1', '177', '1711', '445', '0', 'N', 'N', 'Y', 'Y', 'Y', 'Y', '', '', '0', '1135715', '2017-04-01 00:00:00', 'WON', 'D-0730', 'B3-No Roster foun', 'leet group to same depot/OIC:WON TAM TSZ YEUNG X600', '201704', '0800-0829', 'JC20170410P1.5.0', "2017-04-27 16:09:53'"]

['17040200553', 'A', 'A63', 'CASEMASA63', 'EMAS ', 'CAS', 'NT', 'NT/NW', 'CAS', 'NT', 'NT/NW', 'E', 'EMA', 'SLIP', 'CPB2', 'CAS0', '148', 'TMH', '2017-04-02 08:27:56', '2017-04-02 08:27:56', '2017-04-02 08:28:02', '2017-04-02 08:28:02', '2017-04-02 08:28:19', '2017-04-02 08:28:52', '2017-04-02 08:34:58', '2017-04-02 08:58:52', '2017-04-02 09:11:57', '2017-04-02 09:24:59', '1', '422', '3367', '1434', '0', 'N', 'N', 'Y', 'Y', 'Y', 'Y', '', '', '0', '600', '2017-04-01 00:00:00', 'CAS', 'N-OTHERS', 'X600-ROSTER not foun', 'ssign to default 083', '030', '201704', '0800-0829', 'JC20170410P1.5.0', "2017-04-27 16:09:53'"]

['17040200552', 'A', 'A125', 'ALCEMADA125', 'EMAD ', 'ALC', 'HK', 'HK/W', 'ABD', 'HK', 'HK/W', 'E', 'EMA', 'FEVER', 'ALC1', 'ABD0', '495', 'QMH', '2017-04-02 08:27:38', '2017-04-02 08:27:38', '2017-04-02 08:27:47', '2017-04-02 08:27:47', '2017-04-02 08:27:53', '2017-04-02 08:27:54', '2017-04-02 08:36:09', '2017-04-02 08:51:29', '2017-04-02 09:08:26', '2017-04-02 09:22:05', '1', '511', '3251', '920', '0', 'N', 'N', 'Y', 'Y', 'Y', 'Y', '', '', '0', '600', '2017-04-01 00:00:00', 'ALC', 'N-OTHERS', 'X600-ROSTER not foun', 'ssign to default 083', '030', '201704', '0800-0829', 'JC20170410P1.5.0', "2017-04-27 16:09:53'"]

['17040200551', 'A', 'A192', 'POLEMADA192', 'EMAD ', 'POL', 'KL', 'KL/E', 'POL', 'KL', 'KL/E', 'E', 'EMA1', '', 'PLM9', 'POL0', '339', 'UCH', '2017-04-02 08:27:47', '2017-04-02 08:27:47', '2017-04-02 08:27:52', '2017-04-02 08:27:52', '2017-04-02 08:28:35', '2017-04-02 08:28:36', '2017-04-02 08:34:57', '2017-04-02 08:45:18', '2017-04-02 09:05:48', '2017-04-02 09:19:52', '1', '430', '3076', '621', '0', 'N', 'N', 'Y', 'Y', 'N', 'Y', '', '', '1136057', '1136057', '2017-04-01 00:00:00', 'POL', 'N-2030', 'A1-Roster found', '201704', '0800-0829', 'JC20170410P1.5.0', "2017-04-27 16:09:53'"]

其他信息:

我使用csv.reader将原始文件读入python。

  1. 原始文件是.csv文件
  2. 我使用了newline ='',delimited =',', quotechar ='“'
  3. 然后我将.csv文件转换为一个列表 使用.split()
  4. 的字符串

    任何人都可以帮忙:)

    提前致谢!

    Data Sample

1 个答案:

答案 0 :(得分:0)

根据您的评论,您还希望跳过ANAO列,这些列在您的情况下均为零。这个小片段应该适合你 -

f = open('sample.csv', 'r')
raw_data = f.readlines()
stringlist = [line.strip().split(',') for line in raw_data]

chippedlist = []
for line in stringlist:  # for each row of data in datafile
    if '' in line[:39] or '' in line[42:]:  # column AN=40, check whether '' is in the list
        continue
    else:
        chippedlist.append(line[:39] + line[42:])

您可以根据自己的要求进一步扩展。 以下是输入文件中产生的芯片列表 -

['17040200554', 'A', 'A195', 'WONEMADA195', 'EMAD ', 'WON', 'KL', 'KL/E', 'LAI', 'KL', 'KL/W', 'E', 'EMA', '\xa5\xfe\xa8\xad\xa5X\xafl', 'LCK2', 'CHE0', '185', 'CMC', '2017-04-02 08:28:38', '2017-04-02 08:28:38', '2017-04-02 08:28:48', '2017-04-02 08:28:48', '2017-04-02 08:29:04', '2017-04-02 08:29:05', '2017-04-02 08:31:35', '2017-04-02 08:39:00', '2017-04-02 08:43:17', '2017-04-02 08:57:36', '1', '177', '1711', '445', '0', 'N', 'N', 'Y', 'Y', 'Y', 'Y', '1135715', '2017-04-01 00:00:00', 'WON', 'D-0730', '"B3-No Roster found', ' Fleet group to same depot/OIC:WON TAM TSZ YEUNG X600"', '201704', '0800-0829', 'JC20170410P1.5.0', '2017-04-27 16:09:53']
['17040200553', 'A', 'A63', 'CASEMASA63', 'EMAS ', 'CAS', 'NT', 'NT/NW', 'CAS', 'NT', 'NT/NW', 'E', 'EMA', 'SLIP', 'CPB2', 'CAS0', '148', 'TMH', '2017-04-02 08:27:56', '2017-04-02 08:27:56', '2017-04-02 08:28:02', '2017-04-02 08:28:02', '2017-04-02 08:28:19', '2017-04-02 08:28:52', '2017-04-02 08:34:58', '2017-04-02 08:58:52', '2017-04-02 09:11:57', '2017-04-02 09:24:59', '1', '422', '3367', '1434', '0', 'N', 'N', 'Y', 'Y', 'Y', 'Y', '600', '2017-04-01 00:00:00', 'CAS', 'N-OTHERS', '"X600-ROSTER not found', ' assign to default 0830', ' 2030"', '201704', '0800-0829', 'JC20170410P1.5.0', '2017-04-27 16:09:53']
['17040200552', 'A', 'A125', 'ALCEMADA125', 'EMAD ', 'ALC', 'HK', 'HK/W', 'ABD', 'HK', 'HK/W', 'E', 'EMA', 'FEVER', 'ALC1', 'ABD0', '495', 'QMH', '2017-04-02 08:27:38', '2017-04-02 08:27:38', '2017-04-02 08:27:47', '2017-04-02 08:27:47', '2017-04-02 08:27:53', '2017-04-02 08:27:54', '2017-04-02 08:36:09', '2017-04-02 08:51:29', '2017-04-02 09:08:26', '2017-04-02 09:22:05', '1', '511', '3251', '920', '0', 'N', 'N', 'Y', 'Y', 'Y', 'Y', '600', '2017-04-01 00:00:00', 'ALC', 'N-OTHERS', '"X600-ROSTER not found', ' assign to default 0830', ' 2030"', '201704', '0800-0829', 'JC20170410P1.5.0', '2017-04-27 16:09:53']
['17040200550', 'A', 'A266', 'LATEMADA266', 'EMAD ', 'LAT', 'KL', 'KL/E', 'LAT', 'KL', 'KL/E', 'E', 'EMA', 'FOOT PAIN X MOVE', 'KTG2', 'KTG2', '89', 'UCH', '2017-04-02 08:27:24', '2017-04-02 08:27:24', '2017-04-02 08:27:33', '2017-04-02 08:27:33', '2017-04-02 08:27:57', '2017-04-02 08:27:58', '2017-04-02 08:34:30', '2017-04-02 09:27:41', '2017-04-02 09:37:56', '2017-04-02 09:51:05', '1', '426', '4987', '3191', '0', 'N', 'N', 'Y', 'Y', 'Y', 'Y', '600', '2017-04-01 00:00:00', 'LAT', 'N-OTHERS', '"X600-ROSTER not found', ' assign to default 0830', ' 2030"', '201704', '0800-0829', 'JC20170410P1.5.0', '2017-04-27 16:09:53']
['17040200548', 'A', 'A252', 'WONEMADA252', 'EMAD ', 'WON', 'KL', 'KL/E', 'WON', 'KL', 'KL/E', 'E', 'EMAFR', 'FR(SOB)', 'WTS2', 'WON0', '247', 'QEH', '2017-04-02 08:26:24', '2017-04-02 08:26:24', '2017-04-02 08:26:35', '2017-04-02 08:26:35', '2017-04-02 08:27:09', '2017-04-02 08:28:04', '2017-04-02 08:32:52', '2017-04-02 08:50:05', '2017-04-02 09:19:08', '2017-04-02 09:33:12', '1', '388', '3908', '1033', '0', 'N', 'N', 'Y', 'Y', 'Y', 'Y', '600', '2017-04-01 00:00:00', 'WON', 'N-OTHERS', '"X600-ROSTER not found', ' assign to default 0830', ' 2030"', '201704', '0800-0829', 'JC20170410P1.5.0', '2017-04-27 16:09:53']
['17040200545', 'A', 'A93', 'FANEMADA93', 'EMAD ', 'FAN', 'NT', 'NT/NE', 'SHE', 'NT', 'NT/NE', 'E', 'EMAFR', 'FR(COLL)', 'SSI1', 'FAN0', '223', 'NDH', '2017-04-02 08:22:45', '2017-04-02 08:22:45', '2017-04-02 08:22:54', '2017-04-02 08:22:54', '2017-04-02 08:24:04', '2017-04-02 08:24:05', '2017-04-02 08:26:49', '2017-04-02 08:32:37', '2017-04-02 08:46:49', '2017-04-02 09:12:03', '1', '244', '2878', '348', '0', 'N', 'N', 'Y', 'Y', 'Y', 'Y', '1140591', '2017-04-01 00:00:00', 'FAN', 'N-2030', 'A1-Roster found', '201704', '0800-0829', 'JC20170410P1.5.0', '2017-04-27 16:09:53']
['17040200543', 'A', 'A171', 'SAIEMADA171', 'EMAD ', 'SAI', 'HK', 'HK/E', 'SAI', 'HK', 'HK/E', 'E', 'EMAFR', 'FR(SOB)', 'CWN2', 'SKW2', '143', 'PYNEH', '2017-04-02 08:22:18', '2017-04-02 08:22:18', '2017-04-02 08:22:46', '2017-04-02 08:22:47', '2017-04-02 08:22:59', '2017-04-02 08:23:00', '2017-04-02 08:25:56', '2017-04-02 08:41:06', '2017-04-02 08:48:28', '2017-04-02 09:02:49', '1', '218', '2389', '910', '0', 'N', 'N', 'Y', 'Y', 'Y', 'Y', '1137452', '2017-04-01 00:00:00', 'SAI', 'D-0730', '"B3-No Roster found', ' Fleet group to same depot/OIC:SAI KONG KING TONG X600"', '201704', '0800-0829', 'JC20170410P1.5.0', '2017-04-27 16:09:53']
['17040200541', 'A', 'A217', 'KTKEMADA217', 'EMAD ', 'KTK', 'KL', 'KL/E', 'NGA', 'KL', 'KL/E', 'E', 'EMA', '\xa5\xfe\xa8\xad\xb5L\xa4O', 'KBY3', 'KTK0', '233', 'UCH', '2017-04-02 08:19:23', '2017-04-02 08:19:23', '2017-04-02 08:19:29', '2017-04-02 08:19:29', '2017-04-02 08:20:16', '2017-04-02 08:20:17', '2017-04-02 08:24:18', '2017-04-02 08:33:57', '2017-04-02 08:44:47', '2017-04-02 08:59:48', '1', '295', '2371', '579', '0', 'N', 'N', 'Y', 'Y', 'Y', 'Y', '1140660', '2017-04-01 00:00:00', 'KTK', 'N-2030', 'A1-Roster found', '201704', '0800-0829', 'JC20170410P1.5.0', '2017-04-27 16:09:53']
['17040200540', 'A', 'A260', 'TAIEMADA260', 'EMAD ', 'TAI', 'NT', 'NT/NE', 'TAI', 'NT', 'NT/NE', 'E', 'EMA', 'DIZZ', 'TPO1', 'TAI0', '188', 'AHNH', '2017-04-02 08:18:53', '2017-04-02 08:18:53', '2017-04-02 08:18:58', '2017-04-02 08:18:58', '2017-04-02 08:19:12', '2017-04-02 08:19:13', '2017-04-02 08:25:27', '2017-04-02 08:43:38', '2017-04-02 08:48:37', '2017-04-02 09:02:01', '1', '394', '2568', '1091', '0', 'N', 'N', 'Y', 'Y', 'Y', 'Y', '1135828', '2017-04-01 00:00:00', 'TAI', 'D-0730', '"B3-No Roster found', ' Fleet group to same depot/OIC:TAI CHAN CHUNG FU X600"', '201704', '0800-0829', 'JC20170410P1.5.0', '2017-04-27 16:09:53']