Questions tagged [python]

Python, a versatile and dynamic programming language, offers a multitude of functionalities. Known for its easy comprehensibility and efficient syntax enforcement, Python has gained popularity among developers. However, it's important to acknowledge that Python 2 is no longer supported since January 1, 2020. If you have questions regarding specific versions of Python, kindly include the [python-2.7] or [python-3.x] tag. Additionally, when utilizing alternate Python variations such as Jython or PyPy, or incorporating libraries like Pandas or NumPy, please remember to include relevant tags.

What is the appropriate exception to raise when a provided string does not conform to a specific format?

This question is a continuation of a previously asked question. If a string representing an ISBN number, for example 3-528-03851-5, does not match the format X-XXX-XXXXX-X, what type of exception should be raised? ...

Python Selenium not returning any href values

https://i.stack.imgur.com/glIU1.jpgI'm attempting to retrieve the href of the first image from a Google images. I don't need the img src but rather the href of the a tag. Below is my code, it's able to fetch the Selenium element but the htag is returning ...

Grab Text Class - Python Selenium Automation

I am attempting to extract an element from a webpage using Selenium in Python <span class="_2-1_O"><span dir="auto" class="_1jlxc _3Whw5">Ana Smith</span></span> My goal is to extract the name Ana Smith using Python: contact_name ...

How can you establish the duration for which a WebDriver instance in Python - Selenium2 should await the loading of a page before triggering a timeout exception?

I attempted to set up a webdriver with a specified time out, demonstrated in the code below: self.driver = webdriver.Firefox(None, None, 5) Afterwards, I used the driver to retrieve a web page like this: self.driver.get("http://www.google.com") However ...

Is it possible to integrate Scrapy with the Chrome Browser?

Scraping a web page that utilizes javascript-rendered AngularJS can be tricky. The developers of the site have implemented a feature to detect Safari/Firefox in private browsing mode and prevent scraping. Interestingly, this warning does not appear when us ...

Introducing Unpredictable Characters Amidst Characters in a String (Python)

I'm attempting to insert a series of random letters after each letter within a specified range, but the randomness is not consistent. def add_random_letters(original_string,num): new_word = '' for char in original_string: ran ...

Two applications installed on a single server, each running on its own port. The first application is unable to communicate with the second

I have two applications running on the same VPS (Ubuntu) and domain. The first is a Node.js app running on port 80, while the second is a Python (Flask) app running under Apache on port 3000. The Python app has a simple API with one endpoint /test. When I ...

Processing credit card payments using Django (Python) integrated with RedSys gateway

I am looking to set up credit card payments for a Django Project, but I am unsure of where to begin and struggling to find information on how to do it. Which module would you recommend for integrating credit card payments? Is the module well-documented an ...

Scraping the web: Encountering the ' ' tag when extracting data using Beautiful Soup

I'm attempting to extract data from the table on a Wikipedia page Link. When trying to separate each column by ',' to save it in a CSV file, I encounter an error due to the presence of '/n' tags. For example, the output for row 1 ...

Discovering the characteristic values of a logarithmically transformed matrix

Currently I am tackling the challenge of determining the eigenvalues of real symmetric matrices that are poorly conditioned, with elements spanning a wide range from very small to very large values (ranging from 1e-8 to 1e15) and potentially negative. Thes ...

Distinguishing between the CountVectorizer output used as input for TfidfTransformer and the standalone function TfidfTransformer()

Recently, I delved into exploring NLP concepts and decided to enhance my knowledge by following Python tutorials. One tutorial caught my attention when they utilized the sparse matrix of word counts, generated with CountVectorizer, as input for TfidfTransf ...

Leverage the power of the OR operator in a Lambda function for Python web scraping

Referencing the example provided in this link - How to extract html links with a matching word from a website using python I developed a web scraping script to search for specific keywords in both recent and cached versions of a local newspaper. from bs4 ...

Flask Template Failing to Load Local CSS

Hello there, I am currently working on integrating custom CSS into my Flask application. While I had no issues loading Bootstrap, I seem to be facing some difficulties with my local CSS. Below is the method I am using to load my CSS: <link rel="st ...

Creating a Django view function specifically designed for a navigational bar

My page is dedicated solely to the navigation bar, which I include in my base page. The navigation bar features a drop-down menu with links obtained from the database (representing categories). However, I am facing a challenge in calling my view function ...

A step-by-step guide to displaying a list of HTML page titles with clickable links on the homepage using Django

Snapshot of my window view I apologize if this question seems simple, but I am still learning about Django. I am currently in the process of creating a website where articles are stored as template files. My goal is to showcase all article titles on the h ...

Execute CDK Deployment on a Different AWS Account Using a Fargate Task

I have a scenario where I need to deploy a stack using CDK in Account A by running cdk synth and cdk deploy in an ECS Task located in Account B. To enable this, I set up a role in Account A with administrator access and granted permissions to Account B so ...

Issue: Unable to index a 'dict_values' object.Solution: 'dict_values' object does not support

While experimenting with the Kaggle Bag of Words module, I encountered an issue that stems from the following code snippet: kmeans_clustering = KMeans( n_clusters = num_clusters ) idx = kmeans_clustering.fit_predict( word_vectors ) word_centroid_map = dic ...

Searching for a specific set of words within a text file using Python

I am faced with the challenge of extracting all words from a text file that fall between two specific words. For instance, given the following text: askdfghj... Hello world my name is Alex and I am 18 years all ...askdfgj. If my goal is to capture all w ...

"Configuration options" for a Python function

Not sure what to title this, but look at the example below: def example(): """ Good """ pass If I were to print example.__doc__, it would display " Good ". Is it possible to create additional 'variables' like this for other purposes? ...

Create a unique key in Django using the "id" field with the "unique_together" option

Presenting the model class MyModel(models.Model): id = models.CharField(max_length=10, primary_key=True) password = models.CharField(max_length=25) area = models.CharField(max_length=100, primary_key=True) def __unicode__(self): r ...

What is the best method to retrieve all symbols and last prices from this JSON file?

My attempt at this solution didn't yield the desired results. Interestingly, it did work with a different JSON file (). I suspect the issue lies in the differences at the beginning and end of the current file, as there are some additional parts before ...

Something went wrong while attempting to decode JSON in Python due to a missing comma delimiter

How can I send JSON data using sockets in Python? {"receiver": "2", "sender:": 1, "seq_num": 10, "data": "{"iv": "jdjhvwGriJ95kZwgDWlShw==", "ciphertext": "Fg7ugYYAn ...

Is it necessary to call file.close() when encountering a FileNotFoundError exception while closing a file?

As I work on a Python project, I encountered a situation where I need to open a file and display a message if the file does not exist. However, I am unsure whether it is necessary to close the file when an exception occurs. try: file = open(s ...

Utilize Selenium and Python to retrieve a collection of text items from a g element inside an svg tag

I am trying to extract specific text fields that are located within a <g> tag nested inside an <svg> tag. The website I am working on is: . My goal is to input a company name, search for it, expand the last drop-down menu, and extract informati ...

Discovering all numbers within a specific range that conclude with particular digits

Seeking out numbers within a specific range that end with particular digits is my current quest. Consider the following examples: Identify all numbers up to 15 that end with 5. The expected result would be 2, as both 5 and 15 fulfill this criteria. ...

The outcome of the Python web crawling did not meet our expectations

After using Selenium to scrape web data, I encountered an issue where it only displayed 96 out of 346 products instead of the expected 346. Does anyone have suggestions on how to fix this problem? The crawling code is placed right after the loop for clicki ...

What information can be gathered from the Python requests stack trace?

I am encountering an issue while attempting to make a request call to Elasticsearch. I can successfully execute the same call from a basic script. However, when I convert a list into a string and pass it to the payload, I receive the following stack trace: ...

Extracting information from a webpage

I am trying to extract the text from the XPath provided below using Selenium. However, I keep encountering the following traceback error: date = driver.find_element_by_xpath('//[@id="General"]/fieldset/dl/dd[5]/text()').text() print(date) Error ...

Selenium encounters difficulties in extracting data from a table

I am having trouble extracting data from the table located at . The code snippet I am using seems to be unable to access it for some reason. Can anyone suggest why the table scraping is not working? from bs4 import BeautifulSoup from selenium import webd ...

What distinguishes the delete and assign element functions in Python?

When it comes to refreshing a value, I'm curious about the best method to use. myList = [1, 2, 3] If I need to update the value of the list. One option is myList[2] = 5 Alternatively, del myList[2] myList[2] = 5 The elements in the list are objects t ...

Why is my StableBaselines3 PPO model performing rollouts but not learning?

During the learning process of a model, there are two main phases: A rollout phase A learning phase Although my models go through the rollout phase, they never seem to enter the learning phase. This is evident in the text output in a Jupyter Notebook wit ...

Creating a Docker image for CoreOS (CP100A Training)

I am currently working on the labs for "Google Cloud Platform fundamentals" and encountering some issues. Every time I try to use a CoreOS instance to launch a Docker instance, I encounter an error. For instance, in the Cloud SQL lab, there is a step wher ...

Python - arranging a list to solely grow in intervals of 1

In my Python code, I am working with a list of numbers that may contain duplicates. Here is an example: Input values = [1827, 1827, 1829, 1829, 1827, 1827, 1833] If a number is not repeated in the list, it should be the starting number plus the index of ...

Appending the elements of a list after importing them from a file in Python

My goal is to read elements from a file, add each line to print its output. However, I have been unsuccessful in my attempts so far. For example, if I try to access a certain number: with open('test.txt', 'r') as f: for line in f: ...

Python fastAPI and MongoDB environment allows the return of a tuple in the BaseModel list

Currently, my setup includes env with python311, pydantic, fastapi, and mongod. return membercollection(members=await c_members.find(queryparam).to_list(1000)) This code snippet retrieves the following information: members=[membermodel(id='65b3908a77efb45 ...

What is the process of transforming a tree into a straightforward equation?

After analyzing the program here, it is possible to create a tree structure resembling this: https://i.stack.imgur.com/MwbLl.png Main code: def add(x, y): return x + y def sub(x, y): return x - y def mul(x, y): return x * y FUNCTIONS = [add, sub, mul] TER ...

Converting a table into a collection of dictionaries using BeautifulSoup

I am encountering some difficulties in scraping a specific table and converting it into a list of dictionaries. The table I am interested in scraping can be found on this page My objective is to scrape only the "Batting" table. Below is the code I have b ...

Python Error: 'tuple' does not support the 'append' attribute

I've been encountering the same error in my code repeatedly and despite trying various approaches to resolve it, nothing seems to work. Can someone provide assistance? Here's the code snippet: def main(): p.init() screen = p.display.set_mode((WID ...

Using Selenium with Python: Overcoming StaleElementReferenceException when selecting by class

Attempting to create a basic Python script using Selenium, I encountered a StaleElementReferenceException after the loop runs once. Check out the code snippet being executed: from selenium import webdriver browser = webdriver.Firefox() type(browser) bro ...

Filtering options for plotly dashboard in dropdown format

Below is a dataset along with code that generates a chart: Column A Tools Category role figure Occurences After idx 0 PostgreSQL DatabaseHaveWorkedWith Developer, front-end 38.044286 4 False 1 MySQL DatabaseHaveWorkedWith Developer, front-end 45. ...

"Djoser encounters a 400 Bad Request error when attempting to log in users

Currently working on an e-commerce application using django rest framework and vue. Utilizing djoser.authtoken for authentication purposes. Successfully managed to create a user with a POST request to /users/, but encountering issues while attempting to l ...

Selenium can be utilized to scrape data from multiple pages by automating the process of

I'm currently working on a Selenium script to extract all filenames from every directory on a website. My strategy involves creating a list of directory objects and iteratively clicking on each directory in the list to access the filenames. However, a ...

Apply borders to the table provided in the email using Python

Presently, I am utilizing this approach for sending tables in emails via Python instead of attaching them: Send table as an email body (not attachment) in Python import smtplib from smtplib import SMTPException import csv from tabulate import tabulate te ...

transforming log data from JSON format into individual columns, resulting in a separate file for each column

Here is an example from a log file: {"timestamp": "2022-01-14T00:12:21.000", "Field1": 10, "Field_Doc": {"f1": 0}} {"timestamp": "2022-01-18T00:15:51.000", "Field_Doc": {"f ...

Resolving the Django REST problem: Implementing additional actions with ListApiView

Working on a new project utilizing the Django REST API framework. Encountering an error when running the project locally: File "/home/asad/PycharmProjects/microshop/venv/lib/python3.8/site-packages/rest_framework/routers.py", line 153, in get_rou ...

Is there a way to verify whether the author holds a particular role within a list of roles?

During my attempt to create a check function that would work if you had a role in a list of roles (with a user dev ID also included in the function to bypass it), I encountered an issue. mod_roles = [792816637488281642, 792626568488713617, ...

Django: A wide selection of Products available for offer

How can a relationship be established in Django where the Offer model has multiple Products, and each Product belongs to only one Offer? from django.db import models class Offer(models.Model): city = models.CharField( max_le ...

PyQt4 Error: The 'self' attribute is not recognized in the function object

I am facing an issue where I cannot change the values in the GUI table from the main program, resulting in an error. AttributeError: 'function' object has no attribute 'commandTable' Despite attempting the solution provided in this ...

asyncio: Waiting for signal from external thread

Currently, I am in the process of developing a Python application that needs to interact with a machine to carry out time-consuming tasks. While the asyncio module is ideal for network operations, I am facing a challenge when it comes to accessing the seri ...

I'm unable to bring in my package in this context. My current setup involves using Selenium version 4

from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import by serv_obj=Service("C:Driverschromedriver_win32chromedriver.exe") driver=webdriver.Chrome(service=serv_obj) driver. ...

Troubles encountered when trying to save an Excel.Writer document to a different directory

A timed backup system is being developed for an Excel document using Python, since there will be multiple users accessing it. The goal is to alter the file path away from the local directory. Below is the code snippet; import pandas as pd import datetime ...

What is the best way to extract data from various webpages connected to a single source using selenium?

Lately, I've been working on extracting a large amount of pricing data from a website by starting with one page that contains links to each item's individual page. My goal was to automate the process using a script that would click on a specific ...

Encountering Error 401 while attempting to retrieve course materials through Google Classroom API using Python

As a newcomer to Google APIs, I decided to experiment with the Classroom API. While using the provided sample code, I successfully retrieved a list of courses. However, when attempting to access coursework, topics, or announcements for a specific class, I ...

Transform the information into a matrix data structure

I'm working with a dataframe structured like this: df = pd.DataFrame({'isin': ['a', 'a', 'c', 'd','c', 'e', 'd','f','s','d','c',&a ...

Converting HTML table to pandas dataframe: Extracting data from HTML elements

After extracting data from a large table on the web using requests and BeautifulSoup, I encountered an issue with specific parts of the information. Here is a snippet of the table: <table> <tbody> <tr> <td>265</td> <td> ...

A foolproof method for safeguarding the JWT secret key utilized for encoding and decoding token information

I have a Python application in the works with FastApi, utilizing JWT and OAuth2 password flow for user authentication. Following their documentation, upon user login, a token is issued using the HS256 algorithm along with a specific user secret key. This t ...

Flask decorator for rendering JSON views on Google App Engine

Currently, I am attempting to create a view rendering decorator for JSON in Flask by referring to this specific code snippet: http://flask.pocoo.org/snippets/18/ My challenge lies in the necessity of serializing GAE models as JSON, which the regular jsoni ...

Persistence of Flask-SQLAlchemy changes does not apply

Building a web application where user actions can impact bar graphs, some changes are not saved. Reloading the page sometimes shows the updated bar graphs, indicating that the user's actions were saved. However, upon further reloads, the changes may or may ...

Locate and populate an input field with Selenium

I am having trouble creating a bot to automatically fill in the blank on a website. It doesn't seem to be working correctly, and I can't figure out what the issue is. from selenium import webdriver from selenium.webdriver.common.by import By impo ...

Is there a way for me to edit the date range shown on my line graph?

import yfinance as yf import numpy as np import pandas as pd import seaborn as sns SP = yf.Ticker("^GSPC").history(period='1y') Gold = yf.Ticker("GC=F").history(period='1y') Oil = yf.Ticker("CL=F").history(period='1y') SP['P ...

What is preventing me from being able to pip install autopy?

autopy package installation error: Unable to find Rust compiler Consider updating pip with 'pip install --upgrade pip' and retry the installation process. If building from source, ensure a Rust compiler is installed and accessible o ...

What are the best locators to utilize for Python in logging into a website?

I am attempting to log in to the website. Unfortunately, I am encountering an issue with filling in my username and password. Specifically, I am having trouble with entering my username. It seems like I may be selecting the wrong class name, but I am not e ...

How can JSON data that is disorganized and undefined be properly transformed into a DataFrame?

Trying to figure out how to convert improperly parsed JSON data into a Pandas DataFrame has been quite the challenge. Utilizing Python (3.7.1), I attempted to read the JSON data in the usual way. While my code seems to work if I utilize transpose or axis= ...

Leveraging SQL Server File Streaming with Python

My goal is to utilize SQL Server 2017 filestream in a Python environment. Since I rely heavily on SQLAlchemy for functionality, I am seeking a way to integrate filestream support into my workflow. Despite searching, I have not come across any implementatio ...

There seems to be an issue with VS Code: the function filter is not working as expected when applied to the result of

Recently, I've encountered a frustrating error in VS Code that is hindering my ability to create new files or open existing ones. The pop-up error message that appears states (this.configurationService.getValue(...) || []).filter is not a function This er ...

What is the process for invoking an API by including a dictionary in the URL with Python?

When I include the parameters directly in the URL, like this: import requests url = "https://api.apilayer.com/fixer/latest?symbols=GBP&base=USD" payload = { } headers= { "apikey": "*****" } response = requests.reques ...

Encountering a delay on the Ebay website following the login button click using Selenium in Python

While working on code to log in to a website, I have encountered an issue. The code successfully gets the webpage and accepts cookies, but when attempting to click the login button, the page hangs and the login page fails to load. from selenium import webd ...

utilizing a list's values as arguments in Python function calls

In my Python code, I have the following function: from itertools import product as get_cartesian_product from arithmetic import get_iterable_sum def get_number_of_solutions(*possible_variables, target): number_of_solutions = 0 for comb in get_ca ...

Simple ways to bring a function from a parent directory into your Python script

I am facing an issue with importing a function in my Python project. Despite searching through numerous similar questions on SO, I have been unable to find a suitable solution for my specific problem. The existing answers either lack specificity, are over ...

Issue with building PyQt6 PyInstaller application for MacOS (M1 chip) causing inability to launch

My goal is to develop a python desktop application using PyQt6. I attempted to convert it into a clickable app with pyinstaller, but encountered an "app closed unexpectedly" error when trying to run the app from finder. Similarly, running the app from term ...

Change a JSON object with multiple lines into a dictionary in Python

I currently have a file containing data in the form of multiple JSON rows. Although it consists of about 13k rows, below is a shortened example: {"first_name":"John","last_name":"Smith","age":30} {"first_name":"Tim","last_name":"Johnson","age":34} Here i ...

Print the countdown of elements on the Python page by subtracting a specified number from the total count of

I'm looking to implement a page count down feature in my Python script for each page it navigates to. Below are my attempts so far. How can I achieve the desired result? In order to easily keep track of my script's progress, I have used the (len(elem_href ...

The Django server fails to display the CSS files upon activation

Despite having all the static files in place, only the plain HTML loads without any styles for some unknown reason. I've attempted using collectstatic and even setting up a new project, but to no avail. STATIC_URL is also properly specified along with ...

Converting a JSON dataset into various languages of the world

I am in possession of a large JSON dataset containing English conversations and I am curious about potential tools or methods that could facilitate translating them into Arabic. Are there any suggestions? ...

Ways to retrieve the values from a collection of objects inside a Model using Python

In my system, there are two modes available: User and Friends. This represents a one-to-many relationship. My goal is to retrieve the values in the following manner: record = User.query.filter_by(id=7).first() Below are the methods and properties associ ...