[Solved] Can I use ‘,’ instead of ‘as’ python 3

The old syntax is no longer valid. Source In Python 2, the syntax for catching exceptions was except ExceptionType:, or except ExceptionType, target: when the exception object is desired. ExceptionType can be a tuple, as in, for example, except (TypeError, ValueError):. This could result in hard-to-spot bugs: the command except TypeError, ValueError: (note lack of … Read more

[Solved] how to scrape web page that is not written directly using HTML, but is auto-generated using JavaScript? [closed]

Run this script and I suppose it will give you everything the table contains including a csv output. import csv from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Chrome() wait = WebDriverWait(driver, 10) outfile = open(‘table_data.csv’,’w’,newline=””) writer = csv.writer(outfile) driver.get(“http://washingtonmonthly.com/college_guide?ranking=2016-rankings-national-universities”) wait.until(EC.frame_to_be_available_and_switch_to_it(“iFrameResizer0”)) wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ‘table.tablesaw’))) … Read more

[Solved] main program while loop

No, you do not need a main loop if you use the cmd.Cmd class included with Python: #! /usr/bin/env python3 import cmd class App(cmd.Cmd): prompt=”Type in a command: ” def do_a(self, arg): print(‘I am doing whatever action “a” is.’) def do_b(self, arg): print(‘I am doing whatever action “b” is.’) if __name__ == ‘__main__’: App().cmdloop() The … Read more

[Solved] Create email with the firsts letters of the name [closed]

Here’s some “python magic” name = [‘Elon Reeve Musk’] f”{”.join(filter(str.isupper, name[0]))}@company.com”.lower() >>> [email protected] Whether this is better than your method is debatable. Most of the time a few lines of easy to read code is better than a one line hack. My recommendation would be name = [‘Elon Reeve Musk’] initials=””.join(word[0] for word in name[0].split()) … Read more

[Solved] Can’t deal with some complicated laid-out content from a webpage

You can take advantage of CSS selector span[id$=lblResultsRaceName], which finds all spans that’s id ends with lblResultsRaceName and ‘td > span’, which finds all spans that have direct parent <td>: This code snippet will go through all racing result and prints all races: import requests from bs4 import BeautifulSoup url = “https://www.thedogs.com.au/Racing/Results.aspx?SearchDate=3-Jun-2018” def get_info(session,link): session.headers[‘User-Agent’] … Read more

[Solved] How to merge two dictionaries with same keys and keep values that exist in the same key [closed]

def intersect(a, b): a, b = set(a), set(b) return list(a & b) def merge_dicts(d1, d2): return {k: intersect(v1, v2) for (k, v1), v2 in zip(d1.items(), d2.values())} dictionary_1 = {‘A’ :[‘B’,’C’,’D’],’B’ :[‘A’]} dictionary_2 = {‘A’ :[‘A’,’B’], ‘B’ :[‘A’,’F’]} merge = merge_dicts(dictionary_1, dictionary_2) print(merge) # {‘A’: [‘B’], ‘B’: [‘A’]} 4 solved How to merge two dictionaries with … Read more

[Solved] I am trying to swap the two elements in the list, but after the first swap it keeps getting swapped as it fits the condition to be swapped

num = [3, 21, 5, 6, 14, 8, 14, 3] num.reverse() for i in range(len(num)-1): if num[i] % 7 == 0: num[i – 1], num[i] = num[i], num[i – 1] num.reverse() print(“The required answer :”, num) solved I am trying to swap the two elements in the list, but after the first swap it keeps … Read more

[Solved] Rainbow Array on Python

Here is a basic example. rainbow = [‘Program Ended’, ‘Red’, ‘Orange’, ‘Yellow’, ‘Green’, ‘Blue’, ‘Indigo’] user_input = int(input(“Please select a number between 1 and 6: “)) if user_input > 0 and user_input < 7: print(rainbow[user_input]) else: print(“Program ended”) To capture a user’s input, you simply invoke: input() function. To access an array, you can do … Read more

[Solved] How to save data in multiple files on python

Assuming you don’t care what the file names are, you could write each batch of messages to a new temp file, thus: import tempfile texts = some_function_grabbing_text() while texts: with tempfile.TemporaryFile() as fp: fp.write(texts) texts = some_function_grabbing_text() solved How to save data in multiple files on python

[Solved] How to make a list of variables

Why not use a dictionary for that? This way you could add and remove values in/from your dictionary just as you wanted. my_dict = { ‘var1’: 0, ‘var2’: 0, … } You can delete by doing my_dict.pop(‘var1’) if you need the value back or del my_dict[‘var1’] otherwise and add items with my_dict[‘var3’] = 0. 2 … Read more