如何识别csv行中的单元格数? [英] How do I identify number of cells in a csv line?

查看:147
本文介绍了如何识别csv行中的单元格数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读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个单元格。

解决方案

找到解决方案。



找出一个嵌套列表(列表中的列表)。我能够使用len来识别子列表的长度,然后进一步处理。



谢谢。


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?

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.


这篇关于如何识别csv行中的单元格数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆