如何识别csv行中的单元格数? [英] How do I identify number of cells in a csv line?
问题描述
我正在阅读csv数据,其中一些行可能有5个单元格,其中一些可能有6个。我想检查一个特定行是否有5个单元格或6个?
我正在使用python。
我尝试过:
以下是我的代码到目前为止:
而_i< _len:
如果str(_data [_i] [2])。lower()。strip()=='xxxx':
_summary = str(_data [_i] [1])。lower()+' - '+ str(_data [_i] [ 0])。lower()
如果str(_data [_i] [5])。strip()in(,None):
_description ='Anything:'+ str(_data [_i ] [0])。lower()+'\ n ABC:'+ str(_data [_i] [1])。lower()+'\ n DEF:'+ str(_data [_i] [2] ).lower()+'\ n GHI:'+ str(_data [_i] [3])。lower()+'\ n分类:'+ str(_data [_i] [4])。lower( )'''
我正在检查某个单元格是否为空的部分正在工作并且如果只有行则抛出索引超出绑定错误有5个单元格。
I am reading a csv data and some of the row may have 5 cells and some of them may have 6. I want to check if a particular row has 5 cell or 6? 这篇关于如何识别csv行中的单元格数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
找出一个嵌套列表(列表中的列表)。我能够使用len来识别子列表的长度,然后进一步处理。
谢谢。
I am using python.
What I have tried:
Below is my code so far:
while _i<_len:
if str(_data[_i][2]).lower().strip() == 'xxxx':
_summary = str(_data[_i][1]).lower() + ' - '+ str(_data[_i][0]).lower()
if str(_data[_i][5]).strip() in ("",None):
_description = 'Anything: '+str(_data[_i][0]).lower()+'\n ABC: '+str(_data[_i][1]).lower()+'\n DEF: '+str(_data[_i][2]).lower()+'\n GHI: '+str(_data[_i][3]).lower()+'\n Category: '+str(_data[_i][4]).lower()'''
The part where I am checking if certain cell is empty is working and throwing index out of bound error if row only has 5 cells.Found the solution.
Figured out its a nested list (a list within list). I was able to use len to identify the length of the sublist and then process it further.
Thanks you.