How can I extract numbers based on context of the sentence in python?

2024/11/16 1:23:42

I tried using regular expressions but it doesn't do it with any context

Examples:: "250 kg Oranges for Sale" "I want to sell 100kg of Onions at 100 per kg"

Answer

You can do something like this. First you split the text in words and then you try to convert each word to a number. If the word can be converted to a number, it is a number and if you are sure that a quantity is always followed by the word "kg", once you find the number you can test if the next word is "kg". Then, depending on the result, you add the value to the respective array. In this particular case, you have to assure the numbers are written alone (e.g. "100 kg" and not "100kg") otherwise it will not be converted.

string = "250 kg Oranges for Sale. I want to sell 100 kg of Onions at 100 per kg."# Split the text
words_list = string.split(" ")
print(words_list)# Find which words are numbers
quantity_array = []
price_array = []
for i in range(len(words_list)):try:number = int(words_list[i])# Is it a price or a quantity?if words_list[i + 1] == 'kg':quantity_array.append(number)else:price_array.append(number)except ValueError:print("\'%s\' is not a number" % words_list[i])# Get the results
print(quantity_array)
print(price_array)
https://en.xdnf.cn/q/118987.html

Related Q&A

Chart with secondary y-axis and x-axis as dates

Im trying to create a chart in openpyxl with a secondary y-axis and an DateAxis for the x-values.For this MWE, Ive adapted the secondary axis example with the DateAxis example.from datetime import date…

Env var is defined on docker but returns None

I am working on a docker image I created using firesh/nginx-lua (The Linux distribution is Alpine): FROM firesh/nginx-luaCOPY ./nginx.conf /etc/nginx COPY ./handler.lua /etc/nginx/ COPY ./env_var_echo.…

No Browser is Open issue is coming when running the Robot framework script

I have created Test.py file which has some function in it and using those function names as Keywords in sample.robot file. Ex: - Test.pydef Launch_URL(url):driver.get(url)def article(publication): do…

How to run python file in Tkinter

Im a beginner in Python, hence the question. i would like to run a python file (smileA.py) in Tkinter. How would i start? I do not wish for it to run when clicking a button, but the file to run automa…

Discord.py Cogs “Command [ ] is not found”

I am recoding my discord.py bot using cogs as it wasnt very nice before. I tried this code: import discord import os import keep_alive from discord.ext import commands from discord.ext.commands import …

Binomial distribution CDF using scipy.stats.binom.cdf [duplicate]

This question already has answers here:Alternatives for returning multiple values from a Python function [closed](14 answers)Closed 2 years ago.I wrote below code to use binomial distribution CDF (by u…

How to get Cartesian product of two iterables when one of them is infinite

Lets say I have two iterables, one finite and one infinite:import itertoolsteams = [A, B, C] steps = itertools.count(0, 100)I was wondering if I can avoid the nested for loop and use one of the infinit…

Function to create nested dictionary from lists [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Pygame not using specified font

So I am having a problem in pygame where I specify the font and size to use, but when my program is run, the font and size are the default.Here is where I define the textdef font(self):*****FATAL - THI…

port management in python/flask application

I am writing a REST API using the micro framework Flask with python programming language. In the debug mode the application detect any change in source code and restart itself using the same host and p…