I want to do an exact match

Asked 2 years ago, Updated 2 years ago, 34 views

I'm a beginner.
I want to get a string in the line where the text file (a.txt) exactly matches the string.

a.txt
1,ringo,250
2,banana,100
3,mikan,150

If the string is ringo, I want to get the last 250.

python python3

2022-09-30 14:58

1 Answers

How about something like this for orthodox?
The first number, 1, 2, and 3, are omitted.If not omitted, the index specification will be increased by one.

b=open("a.txt", "r")
data=b.readlines()
It is a 3.x series that uses #dict inclusion notation.
dataas={i.rstrip().split(",")[0]: i.rstrip().split(",")[1]for i in data}
print(datas)

print(datas["ringo"]
print(data["banana"])
print(datas["mikan"])

Run Results

{'ringo':'250', 'banana':'100', 'mikan':'150'}
250
100
150

Once you have created a dictionary-type object and made it correspond to it, you can put the name of any fruit in the argument.If you want the results to be of type int,

 int (datas["mikan"])

Pass it to a constructor of type int as shown in .

addition:
If you want to make sure that the key elements you searched for are not present at all,
Please write it like this.It means searching, so as a handler to search,
Attempted to deploy input.If you're thinking of another way, do so.

b=open("a.txt", "r")
data=b.readlines()
It is a 3.x series that uses #dict inclusion notation.
dataas={i.rstrip().split(",")[0]: i.rstrip().split(",")[1]for i in data}
print(datas)    
print(datas["ringo"]
print(data["banana"])
print(datas["mikan"])
b.close()

# Define a new function, specify a key, and process internally.
def key_error_cover(key):
    try:
       datas [key]
    exceptKeyError as:         
        # If you search for something that is not registered, make an exception   
        print(key, "is not registered")            
        new_value = input("Please, write new value >")
        datas [key] = new_value    
    finally:
        # This way, with or without exceptions, you will always have code that will be executed at the end.
        return data [key]



name = input("please, write the name of your favorite fruit >")
# Give the function the name you want to search and replace the result with the fruit variable
fruit=key_error_cover(name)

# Determine what the corresponding value is
print("What is the value?", fruit) 

# Update the contents of Notepad.   
b=open("a.txt", "w")
for i in list (datas.keys()):
    b.Writerines(i+", "+data[i]+"\n")    
b.close()

input allows users to write new data.
I think where to write depends on the editor and the version.In some cases, the input screen may appear, or the type of question you type directly into .
I type directly into , but I think beginners will be confused at first about where to write.As a guide, please write the appropriate name at the end of the message below, and press Enter.Please don't write it randomly.This is a joke.It's easy to modify even if you write it, just open the .txt file and rewrite it.It's been a little longer, but please try deciphering it.If you want to rewrite the wrong content, I think it would be good to make it more as a program.

Please write a name that does not exist on purpose, and then insert the corresponding value.
After that, if you open or update the txt file, I think the contents will be rewritten.
By the way, I'm sorry to repeat this, but I left out all the first numbers.
(I thought it would be meaningless to search if the number was in the txt file.If it makes sense, please let me know again.)

I'm sorry.
If you want to put the word "Not Applicable" in the letter
except KeyError as:
If you write print("Not Applicable") under and remove any further unnecessary actions, you will get the desired result.


2022-09-30 14:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.