python Take it out of a word file that is worth the number you entered and display it

Asked 2 years ago, Updated 2 years ago, 17 views

I would like to create a program under the following conditions.

  • Search function to search for corresponding items from the price entered
    (Argument: Price return value: corresponding item data (List))
    対応 Corresponding item data (List): List containing price specified by argument
    *Note that there are ',' and ' ' ' ' because it is a csv file
  • Goods function to display search results based on product data
    (Argument: List) Return value: None)
  • If the price entered does not exist, you may omit it.
  • Do not use import

Here's the ideal result.

Enter Price - > 740
Price: 740
Type: Fruit
Item Name: Nectar

I don't know what to do from here.

Current source code:

 path=Goods.CSV
def Search (price):
    with open(path, "r") as f:
        For lin f:
            print(l)

def Goods():

price=input("Enter Price ->")

Goods.CSV

 0, "640", "Apple", "Fruit", "Apple"
0, "440", "Vine", "Fruit", "Vine"
0, "140", "Nashi", "Fruit", "Pear".
0, "740", "Tangerine", "Fruit", "Nectar"
1. "220", "Cucumber", "Vegetables", "Porkmelon"
1. "655", "Strawberry", "Vegetables", "Strawberry"
1, "340", "Watermelon", "Vegetables", "Watermelon"

python

2022-09-30 16:55

2 Answers

If you can't import libraries, you can read them one line at a time from CSV and list them based on the delimiter, and then hand them over to the filter function to process them.


2022-09-30 16:55

If you do not use import (=no modules or libraries) you will have to build the full functionality and specifications of the expected modules or libraries, or you will only create conditionally limited functionality and specifications.

If it's some kind of course or tutorial assignment, it's better to review the information and history because it's based on the content that was previously explained.
It would be inefficient to try various materials and articles that you can see without going through those steps.
@Takahiro Funahashi It would be good to try a course or tutorial where you can understand the basics if you follow through the comments and the previous answers.
When you're done with the basics, try books and classes with various algorithms and examples.

You don't have to worry about it because it's only done once, but it seems redundant to read files in the Search function, so it's better to read them first and make a two-dimensional list.
The following is true:

 path='Goods.CSV'
with open(path, 'r', newline=', encoding='utf-8') asf:
    data = [ ]
    for lin f:#### Take it out line by line
        csv=l.strip().split(',')##### Remove line feed code and divide by comma to make one-dimensional list
        singleline=[ ]
        for dincsv:####Element-by-element extraction processing
            US>singleline.append(d.trip('"))#### If there is a " before or after each value, delete it and add the value to the one-dimensional list
        data.append(singleline)#### After processing one line, add it to data to make a two-dimensional list

def Search (price):
    result=None####Initial value is None
    for ind data:#### Loop for 2D list minutes
        if d[1]==price:####Determine if the price field is the same
            result=d#### If the price is the same, store the appropriate 1D list in the results
            break##### Loop termination
    return result#### Exit result as return value

def Goods(info):#### Display corresponding items based on passed one-dimensional list
    print(f'Price: {info[1]}')
    print(f'type: {info[3]}')
    print(f'Product name: {info[4]}')

price=input("Enter Price ->")
result=Search(price)
if result is not None:
    Goods (result)
else:
    print('There is no item at that price')

The variation is to prepare two lists of prices and a complete list, get the index of the number of data in the list of prices, and get the relevant data from the entire list.

pricelist=[r[1]for lin data]###########################################################
                                      #### Make the index on the price list the same as the index on the data.

def Search (price):
    #### Get the index on the list of prices, get the list from the corresponding index of data, and return None if not returned.
    return data [pricelist.index(price)] if price in pricelist else None

Or, you can make it into a dictionary as you see in the comments.

datadict=dict([(z[1],z)for zin data])####Create a dictionary with the price key at the end of with

def Search (price):
    #### get a list of the appropriate prices from the dictionary or return None
    return datadict [price] if price in datadict else None

Reading CSV files and listing them in two dimensions can also be shortened instead of for loops.

with open(path, 'r', newline=', encoding='utf-8') asf:
    lines=f.read().splitlines()####List the string by line with the new line code removed
    csv = [l.split(',') for line lines] #### Comma Split to 2D List
    data = [list(map(lambday:y.strip('', x)) for x incsv] #### Delete if " appears before or after each value

If you want to shorten it, you can also combine the dictionary into one line as follows:

with open(path, 'r', newline=', encoding='utf-8') asf:
    datadict=dict([(y[1], y)for y in [list(map(lambdax:x.strip('", l.split(','))))for lin f.read().splitlines()]])


2022-09-30 16:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.