Easiest way to cross-reference a CSV file with a text file for common strings

后端 未结 1 1032
梦如初夏
梦如初夏 2021-01-24 20:30

I have a list of strings in a CSV file, and another text file that I would like to search for these strings. The CSV file has just the strings that I am interested in, but the t

相关标签:
1条回答
  • 2021-01-24 20:53

    I would use Python for this. To print the matching lines, you could do this:

    import csv
    with open("strings.csv") as csvfile: 
        reader = csv.reader(csvfile)
        searchstrings = {row[0] for row in reader}   # Construct a set of keywords
    with open("text.txt") as txtfile:
        for number, line in enumerate(txtfile):
            for needle in searchstrings:
                if needle in line: 
                    print("Line {0}: {1}".format(number, line.strip()))
                    break   # only necessary if there are several matches per line
    
    0 讨论(0)
提交回复
热议问题