搜索CSV文件(Python) [英] Searching CSV Files (Python)

查看:135
本文介绍了搜索CSV文件(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经制作好了该CSV文件以供使用..根据我之前所讲的内容,我非常确定此CSV文件是有效的,并且可以在本示例中使用.

I've made this CSV file up to play with.. From what I've been told before, I'm pretty sure this CSV file is valid and can be used in this example.

基本上我有这个CSV文件"book_list.csv":

Basically I have this CSV file 'book_list.csv':

  name,author,year
  Lord of the Rings: The Fellowship of the Ring,J. R. R. Tolkien,1954
  Nineteen Eighty-Four,George Orwell,1984
  Lord of the Rings: The Return of the King,J. R. R. Tolkien,1954
  Animal Farm,George Orwell,1945
  Lord of the Rings: The Two Towers, J. R. R. Tolkien, 1954

我还有一个文本文件"search_query.txt",通过该文件我可以在CSV文件中输入要搜索的关键字或搜索字词:

And I also have this text file 'search_query.txt', whereby I put in keywords or search terms I want to search for in the CSV file:

  Lord
  Rings
  Animal

我目前想出了一些代码(借助我读过的东西),可以让我计算出匹配条目的数量.然后,我让程序编写一个单独的CSV文件'results.csv',该文件仅返回'Matching'或''.

I've currently come up with some code (with the help of stuff I've read) that allows me to count the number of matching entries. I then have the program write a separate CSV file 'results.csv' which just returns either 'Matching' or ' '.

然后,程序将获取此"results.csv"文件,并对我有多少匹配"结果进行计数,并打印计数.

The program then takes this 'results.csv' file and counts how many 'Matching' results I have and it prints the count.

import csv
import collections

f1 = file('book_list.csv', 'r')
f2 = file('search_query.txt', 'r')
f3 = file('results.csv', 'w')

c1 = csv.reader(f1)
c2 = csv.reader(f2)
c3 = csv.writer(f3)

input = [row for row in c2]

for booklist_row in c1:
    row = 1
    found = False
    for input_row in input:
        results_row = []
        if input_row[0] in booklist_row[0]:
            results_row.append('Matching')
            found = True
            break
        row = row + 1
    if not found:
        results_row.append('')
    c3.writerow(results_row)

f1.close()
f2.close()
f3.close()

d = collections.defaultdict(int)
with open("results.csv", "rb") as info:
    reader = csv.reader(info)
    for row in reader:
        for matches in row:
            matches = matches.strip()
            if matches:
                d[matches] += 1
    results = [(matches, count) for matches, count in d.iteritems() if count >= 1]
    results.sort(key=lambda x: x[1], reverse=True)
    for matches, count in results:
        print 'There are', count, 'matching results'+'.'

在这种情况下,我的输出返回:

In this case, my output returns:

There are 4 matching results.

我敢肯定,有更好的方法可以做到这一点,并且避免编写一个完全独立的CSV文件.但这对我来说更容易动手.

I'm sure there is a better way of doing this and avoiding writing a completely separate CSV file.. but this was easier for me to get my head around.

我的问题是,我编写的这段代码仅返回有多少个匹配结果.如何修改它以便也返回ACTUAL结果?

My question is, this code that I've put together only returns how many matching results there are.. how do I modify it in order to return the ACTUAL results as well?

即我希望我的输出返回:

i.e. I want my output to return:

There are 4 matching results.

Lord of the Rings: The Fellowship of the Ring
Lord of the Rings: The Return of the King
Animal Farm
Lord of the Rings: The Two Towers

正如我所说,我敢肯定有一种更容易的方法来做我已经拥有的东西.因此,一些见解会有所帮助. :)

As I said, I'm sure there's a much easier way to do what I already have.. so some insight would be helpful. :)

干杯!

我刚刚意识到,如果我的关键字是小写字母,它将无法正常工作.是否有一种避免区分大小写的方法?

I just realized that if my keywords were in lower case, it won't work.. is there a way to avoid case-sensitivity?

推荐答案

  1. 丢弃查询文件,然后从sys.argv [1:]获取搜索词.

  1. Throw away the query file and get your search terms from sys.argv[1:] instead.

丢弃您的输出文件,而改用sys.stdout.

Throw away your output file and use sys.stdout instead.

将匹配的书本标题附加到result_list.您当前拥有的result_row的名称具有误导性.您想要的计数是len(result_list).打印出来.然后打印result_list的内容.

Append matched booklist titles to a result_list. The result_row that you currently have has a rather misleading name. The count that you want is len(result_list). Print that. Then print the contents of result_list.

将查询词一次转换为小写(在开始读取输入文件之前).在阅读每个book_list行时,将其标题转换为小写.使用小写查询词和小写标题进行匹配.

Convert your query words to lowercase once (before you start reading the input file). As you read each book_list row, convert its title to lowercase. Do your your matching with the lowercase query words and the lowercase title.

这篇关于搜索CSV文件(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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