[Solved] How can I write a python code where it extracts full names from a list of names

[ad_1] One way: storeFullNames = [] storeFullNames.append(‘Jane Doe’) storeFullNames.append(‘Ray Charles’) print(storeFullNames) Second way: storeListNames = [] for name in names: if len(name.split(‘ ‘)) > 1: storeListNames.append(name) print(storeListNames) both ways return a list with full names only… [ad_2] solved How can I write a python code where it extracts full names from a list of names

[Solved] Split text into chapters and store it in txt files [closed]

[ad_1] This Works for me: import re book = open(“book.txt”, “r”) #Here we open the book, in this case it is called book.txt book = str(book.read()) #this is now assigning book the value of book.txt, not just the location chapters = re.split(“Chapter “, book, flags = re.IGNORECASE) #Finds all the chapter markers in the book … Read more

[Solved] library for string comparison in python [closed]

[ad_1] Not a library but an easy way to do what you want: string_a=”ABCD” string_b = ‘ADCT’ print ([ind for ind,char in enumerate(string_a) if string_b[ind] != char]) [1, 3] enumerate gives you the index of each char through the string, if string_b[ind] != char checks if the chars at corresponding indexes are not the same. … Read more

[Solved] How do i grab 80 and 443 out of tag

[ad_1] # If Your Looking To Parse An .html File from bs4 import BeautifulSoup with open(‘test.html’) as html_file: soup = BeautifulSoup(html_file, ‘html.parser’) ul = soup.find(‘ul’, {‘class’, ‘ports’}) a = ul.findAll(‘a’) Ports=[] for port in a: Ports.append(port.string) # If Your Looking To Parse A Website from bs4 import BeautifulSoup import requests session=requests.session() endpoint = LINK response … Read more

[Solved] How to extract URL from HTML anchor element using Python3? [closed]

[ad_1] You can use built-in xml.etree.ElementTree instead: >>> import xml.etree.ElementTree as ET >>> url=”<a rel=”nofollow” href=”https://stackoverflow.com/example/hello/get/9f676bac2bb3.zip”>XYZ</a>” >>> ET.fromstring(url).attrib.get(‘href’) “https://stackoverflow.com/example/hello/get/9f676bac2bb3.zip” This works on this particular example, but xml.etree.ElementTree is not an HTML parser. Consider using BeautifulSoup: >>> from bs4 import BeautifulSoup >>> BeautifulSoup(url).a.get(‘href’) “https://stackoverflow.com/example/hello/get/9f676bac2bb3.zip” Or, lxml.html: >>> import lxml.html >>> lxml.html.fromstring(url).attrib.get(‘href’) “https://stackoverflow.com/example/hello/get/9f676bac2bb3.zip” Personally, I prefer BeautifulSoup … Read more

[Solved] Append float data at the end of each line in a text file

[ad_1] I would do it following way: Assume that you have file input.txt: 520.980000 172.900000 357.440000 320.980000 192.900000 357.441000 325.980000 172.900000 87.440000 then: from decimal import Decimal import re counter = Decimal(‘1.0’) def get_number(_): global counter counter += Decimal(‘0.1′) return ” “+str(counter)+’\n’ with open(“input.txt”,”r”) as f: data = f.read() out = re.sub(‘\n’,get_number,data) with open(“output.txt”,”w”) as … Read more

[Solved] Modify list in Python

[ad_1] Something like this using a generator function: lis = [‘ALRAGUL’,’AKALH’, “AL”, ‘H’,’ALH’ ,’to7a’,’ALRAGULH’] def solve(lis): for x in lis: if x.startswith(“AL”) and x.endswith(“H”): yield x[:2] if len(x)>4: yield x[2:-1] yield x[-1] elif x.startswith(“AL”): yield x[:2] if len(x)>2: yield x[2:] elif x.endswith(“H”): if len(x)>1: yield x[:-1] yield x[-1] else: yield x new_lis = list(solve(lis)) print … Read more