I get a dynamic data string that I capture from the HTTP header, assigning a variable according to the example below:
lang = 'pt-br,pt;q=0.9,en-us;q=0.8,en;q=0.7,fr;q=0.6'
I need to delete the entire string (,pt;q=0.9,en-us;q=0.8,en;q=0.7,fr;q=0.6) leaving only the Language Content as the example:
pt-br
How to exemplify this using the regular expressions module in python (re) ?
Can spilt at
;
>>> lang = 'pt-br,pt;q=0.9,en-us;q=0.8,en;q=0.7,fr;q=0.6'
>>> lang.split(';')[0]
'pt-br,pt'
As this is a
HTTP Accept-Language header
,so can contain more languages.
So then just spilt one more time at
,
>>> lang = 'en-US,fr-CA;q=0.9,en-us;q=0.8,en;q=0.7,fr;q=0.6'
>>> lang.split(';')[0]
'en-US,fr-CA'
>>> lang.split(';')[0].split(',')
['en-US', 'fr-CA']
Then this list of than can be checked if ok in not a
406 Not Acceptable status code
can be send.
So are still working on your web-framework stuff?
As mention before this is very hard task with limited basic Python skill.
If you want some inspiration look at
FastAPI .
It's by far the best new web-framwork for Python,it's a how a modern framework should be build.
(Feb-24-2021, 04:56 PM)snippsat Wrote: [ -> ]Can spilt at ;
>>> lang = 'pt-br,pt;q=0.9,en-us;q=0.8,en;q=0.7,fr;q=0.6'
>>> lang.split(';')[0]
'pt-br,pt'
As this is a HTTP Accept-Language header
,so can contain more languages.
So then just spilt one more time at ,
>>> lang = 'en-US,fr-CA;q=0.9,en-us;q=0.8,en;q=0.7,fr;q=0.6'
>>> lang.split(';')[0]
'en-US,fr-CA'
>>> lang.split(';')[0].split(',')
['en-US', 'fr-CA']
Then this list of than can be checked if ok in not a 406 Not Acceptable status code
can be send.
So are still working on your web-framework stuff?
As mention before this is very hard task with limited basic Python skill.
If you want some inspiration look at FastAPI .
It's by far the best new web-framwork for Python,it's a how a modern framework should be build.
I even thank you for the collaboration but I need it to be in regex and have only the result (pt-br)
Yes, I'm still working on the web framework, soon I'll be publishing here in the community.
As the format of Language header is always same can just split one more time no need for regex.
>>> lang = 'pt-br,pt;q=0.9,en-us;q=0.8,en;q=0.7,fr;q=0.6'
[python]>>> lang = 'pt-br,pt;q=0.9,en-us;q=0.8,en;q=0.7,fr;q=0.6'
>>> lang.split(';')[0].split(',')[0]
'pt-br'
>>> lang.split(';')[0].split(',')[1]
'pt'
>>>
>>> lang = 'en-US,fr-CA;q=0.9,en-us;q=0.8,en;q=0.7,fr;q=0.6'
>>> lang.split(';')[0].split(',')[0]
'en-US'
>>> lang.split(';')[0].split(',')[1]
'fr-CA'
It's usually one or two languages specified or
*
any language.
Accept-Language