python csv headers [英] python csv headers

查看:367
本文介绍了python csv headers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组csv头,我想与上传匹配,它不是真的工作。

 

reader = csv.DictReader(open(PathFile))
header = reader.fieldnames
用于sorted(set(headers))中的头:
if(header =='ip')或(header =='IP'):
打印在标题中找到IP

在这种情况下,IP不是

 
在读取器中的行:
如果row.get('IP'):
print在行中找到IP

未找到。我在这个网站上搜索过,有:

 
IP = row.get('IP',None)
<



uplaod文件,我用

测试它。pre>

<

 
电子邮件,IP,名称,城市,州,邮政编码,国家,垃圾
ghfddgf@gfgs.com, chicago,il,60601,us,erw ewr
5t4g@fdsf.com,34.45.23.34,Mr 5t,NY,NY,10101,us,er


解决方案

根据您的编辑,您需要跳过逗号后的初始空格。



这样做:

 >>> reader = csv.DictReader(open(PathFile),skipinitialspace = True)


I have set of csv headers that i am trying to match with uploads, it's not really working. Not all headers are required, just have to match what's in file


    reader = csv.DictReader(open(PathFile))
    headers = reader.fieldnames
    for header in sorted(set(headers)):
         if (header == 'ip') or (header == 'IP'):
             print  "IP found in Header"

in this case, IP is not

    for row in reader:
         if row.get('IP'):
             print  "IP found in Row"

not found again. I did search on this site, there was:

IP=row.get('IP', None)

that did not work either.

uplaod file, i am testing it with is

Email, IP, Name, City, State, zip, country, garbage
ghfddgf@gfgs.com, 34.4.34.34,Mr GH, chicago, il ,60601, us,erw ewr 
5t4g@fdsf.com, 34.45.23.34, Mr 5t,NY,NY,10101, us, er

解决方案

Based on your edit, you need to skip the initial space after the comma.

This should do it:

>>> reader = csv.DictReader(open(PathFile),skipinitialspace=True)

这篇关于python csv headers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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