【问题标题】:Searching two csv files for matching variables using Python使用 Python 在两个 csv 文件中搜索匹配的变量
【发布时间】:2015-08-08 12:13:40
【问题描述】:

我正在编写一个代码来从一组描述中创建一个完整的玩家列表。拥有一个 CSV 文件,其中包含过去一年中每场 NFL 比赛的描述,例如“44-J.STARKS RIGHT GUARD TO GB 41 FOR 2 YARDS (31-K.CHANCELLOR, 50-K.WRIGHT)”。或“ 3-R.WILSON PASS Short Right to 11-P.HARVIN RAN OB AT SEA 29 FOR 9 YARDS.”我还有一个更新的跑卫列表(以及未来的其他位置),如果球员出现在跑步比赛中,我会手动添加该列表,但已经不在跑卫列表中。我的代码看起来像-

rbs=open('rbs.csv', 'rb+a') #open the running back file
with open('/Users/masongardner/Desktop/pbp-2014.csv', 'rb') as csvfile: #opens the list of descriptions
    reader = csv.reader(csvfile, delimiter=',', quotechar='|')
    reader.next() #skip the title row
    for row in reader:
        desc=row[14] #corresponds with the description of each play
        #print desc
        for line in rbs: #each line should be another running back in the running backs CSV
                if re.findall(line[0], desc, re.I):
                    print 'found'
                    print line
                    print desc
                elif re.findall('no play| KICKS | KNEEL | KNEELS | PUNT | PUNTS | Extra point |Field goal|Two-minute|END GAME|timeout|end of quarter|end quarter|no huddle', desc, re.I):
                    pass
                elif re.findall('right guard|right end|right tackle|left guard|left tackle|left end|up the middle', desc,re.I):
                    print "did not find it"
                    print desc
                    rback=raw_input('running back:')
                    with open('rbs.csv','a') as rbfile:
                        rbwriter=csv.writer(rbfile, delimiter=' ', quotechar='|',quoting=csv.QUOTE_MINIMAL)
                        rbwriter.writerow([rback])
                else:
                    print "did not find it"
                    print desc
    rbs.close()

当我刚刚开始时,我将“rbs”文件的单个输入输入为应该找到的 Eddy Lacy。问题是,当我运行我的代码时,我根本没有收到任何输出。我只返回了 0 的退出状态。

我不明白为什么会这样,因为有数千行,这应该会给我一些输出。有人可以帮我解决我的流程有什么问题吗?

补充: 就目前而言,当我用 textedit 打开它时,我的 rbs.csv 文件的格式是这样的

27-E.LACY

24-F.戈尔

等等

当我查看播放描述的其中一行时,文件的格式如下: 2014090400,2014-09-04,1,13,32,GB,SEA,1,10,39,,0,,0,"(13:32) (NO HUDDLE) 44-J.STARKS RIGHT GUARD TO GB 41 2 码(31-K.CHANCELLOR,50-K.WRIGHT)。",0,,,2014,2,"NO HUDDLE",RUSH,1,0,0,0,,0,0,0,, 0,0,0,0,0,0,"右后卫",39,OWN,0,,0,,0

【问题讨论】:

  • 您应该提供其中一个 CSV 文件的真实摘录,但在我看来,您的引号字符可能是 "(双引号)而不是 |。如果没有真实的文件,就不可能说出您的分隔符字符。 Als 打印出你的 row ,这样你就会看到你实际上是按照你的期望跨过这些线。
  • 当我运行打印命令时,代码确实打印了整个描述文档。如果我在嵌套的 for 循环中也打印 'line' 变量,则这些行会为第一个 rbs 'row' 打印一次,但不会为以下任何行打印一次
  • 我的意思是,代码正在遍历每一行,但仅在第一行的“for line in rbs”之后运行项目

标签: regex python-2.7 csv for-loop


【解决方案1】:

for line in rbs 部分只遍历文件一次的行,并且开头只有一行(尽管找到第二个elif 时可能会添加行。

如果要遍历所有行,则需要在 for 循环中移动该文件的开头。而且不用打开+a。

with open('/Users/masongardner/Desktop/pbp-2014.csv', 'rb') as csvfile: #opens the list of descriptions
    reader = csv.reader(csvfile, delimiter=',', quotechar='|')
    reader.next() #skip the title row
    for row in reader:
        desc=row[14] #corresponds with the description of each play
        #print desc
        with open('rbs.csv', 'rb') as rbs:  #open the running back file
           for line in rbs: #each line should be another running back in the running backs CSV
                if re.findall(line[0], desc, re.I):
                    print 'found'
                    print line
                    print desc
                elif re.findall('no play| KICKS | KNEEL | KNEELS | PUNT | PUNTS | Extra point |Field goal|Two-minute|END GAME|timeout|end of quarter|end quarter|no huddle', desc, re.I):
                    pass
                elif re.findall('right guard|right end|right tackle|left guard|left tackle|left end|up the middle', desc,re.I):
                    print "did not find it"
                    print desc
                    rback=raw_input('running back:')
                    with open('rbs.csv','a') as rbfile:
                        rbwriter=csv.writer(rbfile, delimiter=' ', quotechar='|',quoting=csv.QUOTE_MINIMAL)
                        rbwriter.writerow([rback])
                else:
                    print "did not find it"
                    print desc

或者,您也可以照常打开文件,并在 for line in rbs: 之前查找文件的开头。

这都不是很有效,所以除非您希望rbs.csv 的内容不适合内存,否则我会在内存中的列表中构建该信息并循环检查该信息。并且只在最后写出列表。

一旦事情正常,您应该查看的另一个优化是使用re.compile() 编译您曾经使用过的模式并重新使用它们。那应该加快findall。

另外,您的引号字符似乎是 " 而不是 |,我不确定您为什么要像现在这样设置它,也不知道它为什么会起作用。

【讨论】:

    猜你喜欢
    • 2018-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 2014-11-27
    • 2013-12-21
    相关资源
    最近更新 更多