Home > database >  Search csv and return value next to it
Search csv and return value next to it

Time:11-09

I'm working on a simple data filters and I need help with this task:

Let's say this is my .csv file:

08534710,2888,15
08583315,2999,5

My goal here is to write a function that will search for given value (e.g. 2888) and return value next to it (15) Here's my code so far:

def wordfinder(searchstring):
    csv_file = pd.read_csv('test.csv', "r")
    for searchstring in csv_file:
        if searchstring in csv_file:
            print(searchstring[2])
            return searchstring[2]

But I don't think it works as intended.

CodePudding user response:

Search the searchstring into the second column and return the values of the third column:

def wordfinder(searchstring):
    df = pd.read_csv('test.csv', dtype=str, header=None)
    return df.loc[df[1] == searchstring, 2]

Output:

>>> wordfinder('2888')
0    15
Name: 2, dtype: object

# OR

>>> wordfinder('2888').tolist()
['15']

CodePudding user response:

It's really this simple:

# Open and read the file
with open('t.txt') as f:
    lines = [l.strip() for l in f.readlines()]

def get_field(num):
    for line in lines:
        parts = line.split(',')
        if parts[1] == str(num):
            return int(parts[2])

Usage:

>>> get_field(2888)
15
>>> get_field(2999)
5

CodePudding user response:

I suggest the solution as following when:

  1. this is just a simple search task, which means you don't need a dataframe or importing the whole pandas package; or
  2. you don't know in advance in which column your searchstring appears.
import csv

def word_finder(search_string):
    # Read the CSV file
    csv_reader = csv.reader('test.csv')

    # Loop through every line
    for line in csv_reader:
        
        # If the search_string exists in this line
        if search_string in line:
            
            # Get its position
            position = line.index(search_string)

            # Only take the next value if it exists
            if position   1 < len(line):
                return line[position   1]

    # Not found
    return None

CodePudding user response:

You don't really need pandas for such a simple case. Try this:

search = '2888'
with open('my.csv') as csv:
    for line in csv:
        tokens = line.strip().split(',')
        try:
            i = tokens.index(search)
            print(tokens[i 1])
        except (ValueError, IndexError):
            pass
  • Related