如何删除空白行

How to delete blank row

我将数据保存为csv 格式,但csv 文件包含很多空白行。

如何删除空白行?喜欢来自 csv 吗?

1   7   5.329618927 5.678765376
2   7   3.364469002 4.176536709
3   7   4.214949544 7.205212347
4   7   3.324136778 8.935188439
5   7   4.746355556 3.89335459



1   8   5.312160001 5.65157164
2   8   3.378701171 4.175205323
3   8   4.202770433 7.216973641
4   8   3.32496778  8.924379077
5   8   4.744037125 3.891049294

尝试按照以下方式读取包含空行的 input.csv 文件:

import csv
with open ('input.csv') as csvfile:
    # READ ALL LINES: 
    csvlines = csv.reader(csvfile, delimiter=',')

    # REMOVE BLANK LINES:
    csvlines = [x for x in csvlines if len(x) > 0]  

    # PRINT EACH LINE: 
    for onerow in csvlines:
        print(onerow)

输出:

['1   7   5.329618927 5.678765376']
['2   7   3.364469002 4.176536709']
['3   7   4.214949544 7.205212347']
['4   7   3.324136778 8.935188439']
['5   7   4.746355556 3.89335459']
['1   8   5.312160001 5.65157164']
['2   8   3.378701171 4.175205323']
['3   8   4.202770433 7.216973641']
['4   8   3.32496778  8.924379077']
['5   8   4.744037125 3.891049294']

正在读取显示内容的文件

假设上面的文本是您要导入的文件内容的示例(字段之间有多个空格和一些空行,那么我可能会按如下方式导入它(请注意,这不是'不是真正的 csv 文件):

   ]Dat=: cut;._2 freads 'myfile.txt'
┌─┬─┬───────────┬───────────┐
│1│7│5.329618927│5.678765376│
├─┼─┼───────────┼───────────┤
│2│7│3.364469002│4.176536709│
├─┼─┼───────────┼───────────┤
...
├─┼─┼───────────┼───────────┤
│ │ │           │           │
├─┼─┼───────────┼───────────┤
│1│8│5.312160001│5.65157164 │
├─┼─┼───────────┼───────────┤
...
├─┼─┼───────────┼───────────┤
│5│8│4.744037125│3.891049294│
└─┴─┴───────────┴───────────┘
   Dat -. (4 # a:)  NB. Dat without items consisting of 4 empty boxes
┌─┬─┬───────────┬───────────┐
│1│7│5.329618927│5.678765376│
├─┼─┼───────────┼───────────┤
...
├─┼─┼───────────┼───────────┤
│5│8│4.744037125│3.891049294│
└─┴─┴───────────┴───────────┘
   _999 ". > Dat -. (4 # a:)  NB. unbox and convert to numeric
1 7 5.32962 5.67877
...
5 8 4.74404 3.89105

你可以一行完成:

   ]Dat=: _999 ". > -.&(4 # a:) cut;._2 freads 'myfile.txt'
1 7 5.32962 5.67877
2 7 3.36447 4.17654
3 7 4.21495 7.20521
4 7 3.32414 8.93519
5 7 4.74636 3.89335
1 8 5.31216 5.65157
2 8  3.3787 4.17521
3 8 4.20277 7.21697
4 8 3.32497 8.92438
5 8 4.74404 3.89105

Reading/writing csv 文件

如果您有一些数据想要 save/read 作为 csv 文件,那么 tables/csv addon 会有所帮助。

   load 'tables/csv'
   Dat writecsv jpath '~temp/myfile.csv'  NB. write to J's temp folder
290
   readcsv jpath '~temp/myfile.csv'
┌─┬─┬───────────┬───────────┐
│1│7│5.329618927│5.678765376│
├─┼─┼───────────┼───────────┤
│2│7│3.364469002│4.176536709│
├─┼─┼───────────┼───────────┤
...
├─┼─┼───────────┼───────────┤
│5│8│4.744037125│3.891049294│
└─┴─┴───────────┴───────────┘
   makenum readcsv jpath '~temp/myfile.csv'
1 7 5.32962 5.67877
2 7 3.36447 4.17654
3 7 4.21495 7.20521
4 7 3.32414 8.93519
5 7 4.74636 3.89335
1 8 5.31216 5.65157
2 8  3.3787 4.17521
3 8 4.20277 7.21697
4 8 3.32497 8.92438
5 8 4.74404 3.89105

该插件会自动将超过 2 维的数组重新整形为 2 维,这样就不会出现任何空行。如果您仍然有空行,最好了解为什么要创建它们并防止它们被写入。如果您仍然需要删除它们,那么上面显示的技术将起作用。