Skip to main content

Posts

Showing posts with the label Python

Find first match in string from the end

I have a string Manager of Medical Threat Devlop at Micro by regular expression findall i want to find any words which goes after "at","for", "of", . If to apply r'(?:for|at|of)\s+(.*)' i will get incorrect ['Medical Threat Devlop at Micro'] i want find by regular expression from the end of string ['Micro'] P/S. Please don't advice split More examples: 1)Manager of Medical Threat Devlop at Canno -> Canno 2)Manager of Medicalof Threat Devlop of Canno -> Canno 3)Manager of Medicalfor Threat Devlop for Canno -> Canno 4)Threat Devlop at Canno Matt -> Canno Matt by Vladimir Shabuniayeu in StackOverflow on July 05, 2022 . Answer You can use re.findall(r'.*\b(?:for|at|of)\s+(.*)', text) See the regex demo . Details : .* - any zero or more chars other than line break chars, as many as possible \b - a word boundary (?:for|at|of) - for...

Scrape ajax pages

I do not anything how to scrape ajax pages there is no pagination on website the website will be load by clicking the load more button these is the page link https://aaos22.mapyourshow.com/8_0/explore/exhibitor-gallery.cfm?featured=false import scrapy from scrapy.http import Request from selenium import webdriver from scrapy_selenium import SeleniumRequest import pandas as pd class TestSpider(scrapy.Spider): name = 'test' def start_requests(self): yield SeleniumRequest( url="https://aaos22.mapyourshow.com/8_0/explore/exhibitor-gallery.cfm?featured=false", wait_time=3, screenshot=True, callback=self.parse, dont_filter=True ) def parse(self, response): books = response.xpath("//h3[@class='card-Title\nbreak-word\nf3\nmb1\nmt0']//a//@href").extract() for book in books: url = response.urljoin(book) ...

Remove duplicate index in 2D array

I have this 2D numpy array here: arr = np.array([[1,2], [2,2], [3,2], [4,2], [5,3]]) I would like to delete all duplicates corresponding to the previous index at index 1 and get an output like so: np.array([[1,2], [5,3]]) However, when I try my code it errors. Here is my code: for x in range(0, len(arr)): if arr[x][1] == arr[x-1][1]: arr = np.delete(arr, x, 0) >>> IndexError: index 3 is out of bounds for axis 0 with size 2 by testerboy22 in StackOverflow on July 02, 2022 . Answer Rather than trying to delete from the array, you can use np.unique to find the indices of first occurrences of the unique values in the second columns and use that to pull those values out: import numpy as np arr = np.array([[1,2], [2,2], [3,2], [4,2], [5,3]]) u, i = np.unique(arr[:,1], ret...

Read data from a dictionary in pandas and assign new column value

I have dict data in a pandas column and I am attempting to assign values to a new column from the dict . import pandas as pd df = pd.DataFrame({'n': [np.nan, np.nan], 'd': [{'status': {'version': '1.0.0'}, 'p': [{'identifier': {'Id': 5155, 'fips': '07', 'aId': 5175}, 'lt': {'ltnum': '14', 'ls1': 0.1485537, 'ls2': 6471, 'ptype': 'NO'}}]}, np.nan] }) When I try: df['n'].fillna(df['d'].str['p'][0][0]['lt']['ls1'], inplace=True) I get a: TypeError: 'float' object is not subscriptable by kms in StackOverflow on July 03, 20...

iterate and compare two lists in python, if contains string from the other list, pop item

I'm trying to comparing two lists ( mama and filterkeywords in the code shown), and remove values from the first list depending on the second. I have this code: occurenceList = [] title = ['SL C ','FS D ','FS H','FS F','FS D','SL C','SL F','FS H','SL H','SL D'] mama = ['slow cat Sas','fast dog PoP','fast hen Luke','fast fish Lee','fast dog joe','slow cat yan','slow fish ben','Fast hen Tim','Slow hen Jen','slow dog moe'] filterkeywords = ['hen','dog','fish','Fish'] print(" ") for text in mama: for words in filterkeywords: if words in text: animal = (mama.index(text)) print("index: ", animal, ' | ', mama[animal], " | Word Found: ", words) title.pop(animal) mama.pop(animal) print(" ") for items i...

Extract course ids for instructors from json input

Let's say I've got a nested JSON file as below. If I want to print the courses that each instructor teaches, how do I do that? { "info":{ "source_objects":[ { "type":"sub-category", "id":277438897, } ], "item_type":"course", "items":[ { "_class":"course", "id":156173119, "is_paid":null, "trainer":[ { "id":257585701, "url":"/user/tania_guerra/", } ], { "_class":"course", "id":12456, "is_paid":null, "trainer":[ { "id":257585701, "url":...

Splitting Array and adding splitted part to end

I want to split the array at a point s and add it to the end of the array. But my output is my input list. def split_and_add(arr, s): n = len(arr) if n >= s or s <=0: return arr else: end = [] for i in range(0,s): end = end + [arr[i]] start = [] for i in range(s,n): start = start + [arr[i]] return start + end print(split_and_add([1,2,3,4,5,6],3)) The output is still [1,2,3,4,5,6]. Could somebody help? by MarcelausZell in StackOverflow on June 05, 2022 . Answer Only issue in your code is in line if n >= s or s <=0: Here, you are checking if length of array n is greater than the break point s and if yes, you are returning the originl array. But what you need to do is check if s is greater than or equal to length of array and if yes, return original array. So, all you need to do is replace n >= s with s >= n . So your condit...

Using a variable inside the string for SQL queries using python

i want to see all the data of a particular table of sql database world entered by user, The below code will give me desired output for city table, but i want table name to be entered by user and wants to make my code work for all the cases. from sqlite3 import connect import mysql.connector mydb=mysql.connector.connect(host="localhost",user="root",password="",database='world') mycursor=mydb.cursor() mycursor.execute("select * from city") for i in mycursor: print(i) I thought to declare city as string variable table_name=str(input("enter table name:")) mycursor=mydb.cursor() mycursor.execute("select * from table_name") for i in mycursor: print(i) But i'm getting an error world.table_name doesn't exist, which i understand. is there any way to evolve at solution of my case. looking for kind help, thanks a lot. by def __init__ in StackOverflow on June 05, 2022 . ...

How to parse pandas column in the loop if it's a dictionary

I have the DataFrame, one of the column contains dictionary in rows in format: rates 0 {'time': '2022-06-05T19:25:57.3000000Z', 'asset_id_quote': '0X', 'rate': 73571.98764837519} 1 {'time': '2022-06-05T19:25:57.3000000Z', 'asset_id_quote': '18C', 'rate': 11607635.869234081} 2 {'time': '2022-06-05T19:25:57.3000000Z', 'asset_id_quote': '1EARTH', 'rate': 4162937.862828232} 3 {'time': '2022-06-05T19:25:57.3000000Z', 'asset_id_quote': '1ECO', 'rate': 7337.697146675354} 4 {'time': '2022-06-05T19:25:57.3000000Z', 'asset_id_quote': '1INCH', 'rate': 34645.31024605586} ... ... 49...

How to run word search function for each letter of the alphabet

I have defined a function that searches a string for all 5-letter words starting with the letter I've specified. A text file is then created with the processed list. I currently must run the program manually for each letter. How can I change my code so that the function will search for all letters of the alphabet one after the other and make a separate text file for each result? import re from pathlib import Path # define starting letter of word search ltr = 'm' # define RegEx pattern to find all 5-letter words starting with a certain letter pattern = r'\b'+ltr+r'\w{4}\b' # define Function WordSearch: search wordlist.txt with defined RegEx Pattern def WordSearch(): # set Path to Main Text file file = r'C:\Users\User1\OneDrive\Documents\wordlist.txt' # open Main Text file and set variable 'MainText' equal to contents fileToOpen = Path(file) f = open(fileToOpen) MainText = f.read() # search all words i...

Finding a mismatch between two correlated columns in pandas dataframe

I have a large pandas dataframe which has two columns. One column is the unique code for an item and the second column contains the name of that item. For example purposes lets say 'ID' has a unique code for an animal and 'Name' has the name of that animal. df = pd.DataFrame({'ID': ['AA','BB', 'CC', 'DD', 'EE'], 'Name': ['Ape', 'Bull', 'Chimp', 'Dog', 'Dog']}) ID Name 0 AA Ape 1 BB Bull 2 CC Chimp 3 DD Dog 4 EE Dog When I run the code below I will get false because there is one more unique item in the ID column than in the Name column len(df['Code'].unique()) == len(df['Name'].unique()) My question is how can I find out which animal is being represented by two codes. Ideally I would like to end up with a dataframe that has: ID Name 3 DD Dog 4 EE Dog But as long as I can ide...

Timezone in python date string is not matching

In a script, I am converting the date string into DateTime format, so that I can modify the date, but this timezone part is showing an error. from datetime import datetime date_str = 'Wed, 1 Jun 2022 16:44:40 +0200 (CEST)' temp_date = datetime.strptime(date_str, '%a, %d %b %Y %H:%M:%S %z (%Z)') print(temp_date) When I run this I am getting ValueEror. ValueError: time data 'Wed, 01 Jun 2022 16:44:40 +0200 (CEST)' does not match format '%a, %d %b %Y %H:%M:%S %z (%Z)' by Debashis Panda in StackOverflow on 2022-06-05 . Answer Seeing the complexity of problem, I suggest using third party library like dateutil which can parse datetime with ease. from dateutil.parser import parse date_str = 'Wed, 1 Jun 2022 16:44:40 +0200 (CEST)' temp_date = parse(date_str) print(temp_date) temp_date is of type datetime.datetime https://dateutil.readthedocs.io/en/stable/ by Bijay Regmi ...

How to create a JS file from python graph

I have used altair to create graphs using python. So I have used following code to save the chart. chart.save('chart.html', embed_options={'actions': False}) My chart.html file look as below, <!DOCTYPE html> <html> <head> <style> .error { color: red; } </style> <script type="text/javascript" src="https://cdn.jsdelivr.net/npm//vega@5"></script> <script type="text/javascript" src="https://cdn.jsdelivr.net/npm//vega-lite@4.8.1"></script> <script type="text/javascript" src="https://cdn.jsdelivr.net/npm//vega-embed@6"></script> </head> <body> <div id="vis"></div> <script> (function(vegaEmbed) { var spec = {"config": {"view": {"continuousWidth": 400, "continuousHeight": 300}}, "data": {"url": "https://c...

Looping over vectors in python (analogous to Pandas columns)

I have a number of analogous operations that I would like to perform on different vectors. What is the 'pythonesque' way of 'looping' over these variables to save lines? import numpy as np length = 10 l = np.ones(length, dtype = int) r = np.ones(length, dtype = int) v = np.ones(length, dtype = int) w = np.ones(length, dtype = int) q = np.ones(length, dtype = int) new_l = np.ones(length, dtype = int) new_r = np.ones(length, dtype = int) new_v = np.ones(length, dtype = int) new_w = np.ones(length, dtype = int) new_q = np.ones(length, dtype = int) l = 0.05 * new_l + 0.95 * l r = 0.05 * new_r + 0.95 * r v = 0.05 * new_v + 0.95 * v w = 0.05 * new_w + 0.95 * w q = 0.05 * new_q + 0.95 * q If these were columns in a Pandas dataframe, I would write: import pandas as pd length = 10 df = pd.DataFrame(index = range(length)) for v in ['l', 'r', 'v', 'w', 'q']: df[f'{v}'] = 1 df[f'new_{v}'] = 1 df[...

re get text between braces

Data module All { obj Bucket { Damage = 900 Type = Weapon Cooldown = 2 Craftable = false } } Output "Bucket": { Damage: 900 Type: Weapon Cooldown: 2 Craftable: false } Current Code def get_object(x): pattern = r'\{+(.*?)\}' return re.findall(pattern, x, re.MULTILINE) # returns [] I want to get the data between the { }, this code works for eg. "{ text }" but not for this string, maybe it's because of the newlines, I don't know much regex so any help is appreciated! by Lun in StackOverflow on 2022-06-05 . Answer Use this pattern (?<=obj\s)(\w+)(?:.*?)(\{.*?\}) Also, see the demo Python Example import re text = """module All { obj Bucket { Damage = 900 Type = Weapon Cooldown = 2 Craftable = false } }""" result = re.search(r"(?<=obj\s...