Search for a specific value within a CSV document

I'm having trouble retrieving a specific value from a CSV file based on certain criteria within a function. Although my program successfully fetches all the values, it fails to return the one linked to the input I provided. Any assistance would be greatly appreciated.

An example of the data is as follows:

rose,7.95
lily,3.95
begonia,5.95

The function I have written looks like this:

def retrieve_value(csv_file, flower):
    import csv    

    file = open(csv_file)

    for row in csv.reader(file):
        if flower in row[0]:
            print(row[1])

    file.close()

When I executed the program with the following line:

retrieve_value("flowers.csv","rose") 

The output displayed all the values present in the file:

7.95
3.95
5.95

However, the desired outcome should only show the value associated with the second entry:

7.95

Thank you for your help.

Answer №1

After running the provided code, I got a successful output of 7.95.

Could it be that you mistakenly referenced the wrong function? In your inquiry, the function problem3_7 was mentioned instead of problem3_8

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Python List Reduction through Performing Bitwise OR Operations

I am dealing with two lists consisting of zeros and ones. These lists are always the same length, but the values within can vary. Here is an example, showcasing a scenario where I need a solution that can apply to lists of any size with zeros and ones at a ...

The installation of wxPython for Python 3.10 (32-bit) is currently not possible for Pip

I have set up two versions of Python 3.10 frameworks. I currently have wxPython310 for 64-bit Python, but unfortunately there is no available wxPython for 32-bit Python. Attempting to install wxPython using the link https://wxpython.org/Phoenix/snapshot-b ...

Changing the value of a user object's variable in Django

I have been working on implementing a feature that allows users to edit their personal information in a Django project using Django forms. However, after entering new values in the form and hitting enter, the user is redirected back to the main profile pag ...

Python implementation of the Velocity-Verlet algorithm for a double-well potential

Currently, I am working on implementing the Verlet algorithm to model a double well potential V(x) = x^4-20x^2 in order to create a basic phase portrait. However, the resulting phase portrait has an irregular oval shape and appears to be incorrect. I suspe ...

What is the process for making the functions within a module accessible to a class?

Let's discuss a scenario: I have a module containing various function definitions. I am looking to create a class that can access these functions. Which approach would you recommend: Option 1 or Option 2? import ModuleWithFunctions class MyClass(o ...

Developing a personalized address field in Django Model

What is the standard method for storing postal addresses in Django models? Are there any specialized libraries available that offer custom model fields with built-in validation and formatting for postal address data? If no libraries are found, how can I c ...

Accessing Kucoin's futures API using Python resulted in the following error message: "code": "429000", "msg": "Exceeding Request Limit"

Encountering an issue when submitting an order to the Kucoin exchange through the API. "code":"429000","msg":"Too Many Requests" It appears that my code is sending excessive requests to Kucoin, causing my IP addre ...

Explanation of the trailing axes in numpy broadcasting

Query I would appreciate a detailed explanation of the Numpy array broadcasting rules mentioned in 2012 and an elucidation on what exactly are "trailing axes." I am uncertain about the specific "linked documentation page" referred to in the answer. It&apo ...

Using Python Pandas: Utilizing .apply() to pass multiple arguments or column values to a custom function

https://i.stack.imgur.com/n4O7x.png I'm attempting to create a custom function that takes three inputs and checks if the length of the full name (combination of first and last) is greater than the length of the occupation. I've tried two differe ...

Extracting information from several span elements within the main span class using BeautifulSoup

I'm currently attempting to extract data from the following HTML code: <span class="double-line-ellipsis"> <span> ₹ 2800 for 2 (approx) </span> <span> | </span> <a data-w-onclick="stopClickPropagation|w1-re ...

The challenge of clicking the "ShowMore" button in an infinitely scrolling page is a common issue when using Selenium with Python

I am currently working on extracting Mobile Legend comment data from the site using web scraping. I need my bot to automatically scroll down and load as many comments as possible before scraping all of them. However, I encountered an issue when ...

A guide to extracting information from a dynamic webpage using Selenium

As a beginner in the world of selenium, I am looking to extract the price and offer end time from a Udemy Course link. How can I achieve this? The price and course end time are dynamically loaded onto the website. While I have experience extracting simple ...

CSV file displaying incorrect data due to xPath expression issue

I have written a code to extract data for the values "Exam Code," "Exam Name," and "Total Question." However, I am encountering an issue where the "Exam Code" column in the CSV file is populating with the same value as "Exam Name" instead of the correct ...

What is the best way to locate an element on a website that consistently changes its label ID buried deep within nested DIVs?

I'm facing an issue with my website tracking system, where I am trying to extract shipment details using Selenium in Python and save them to an Excel file. However, I am encountering difficulties with getting Selenium to function properly. It keeps sh ...

How to transfer a variable from an inner loop to an outer loop in Python using pandas?

Having trouble passing the variable of the inner loop to the outer loop in my Python code. Each time the inner loop breaks, the value of variable "x" resets to the initial value. import pandas as pd import csv user_list = pd.read_csv(r'C:\Users& ...

Error encountered: "Flask API deployed on Heroku - <Response [503]> - JSONDecodeError: Expecting value at line 1 and column 1 (character 0)"

Having issues deploying my flask app on Heroku. It runs perfectly fine on my local system, but when deployed and tested on Heroku, I am getting a Response <503>. Sharing the code snippet below import requests import json import time url = 'https ...

What is the best way to craft an XPATH expression in Python and Selenium to extract the text content located inside a span

Currently, I am attempting to extract text from a specific HTML element using XPATH in order to save it as a variable. <span class="a-truncate-cut" aria-hidden="true" style="height: auto;">Champion unisex adult Ameritage ...

Ways to eliminate duplicates while retaining rows with a specific non-null value in another column using Pandas

My database contains multiple duplicate records, some of which have bank accounts. I am looking to retain only the records with a bank account. Essentially, something along the lines of: if there are two Tommy Joes: keep the one with a bank account ...

The issue arises from the misalignment of Altair plots that feature overlaid boundaries and points

I've been facing a challenge with flipping a layer map plot in Altair showcasing London. More details can be found here: Solving upside plot and projection problems in Geopandas and Altair Another issue arises from having a dataframe containing shape ...

Searching for precise string delimited by spaces in Python

Unique Example: words_to_check = ['dog', 'cat', 'bird chirp'] full_word_list = ['doggy dog cat', 'meow cat purr', 'bird chirp tweet tweet', 'chirp bird'] for word in words_to_check: ...