How can I extract a number from a string using selenium? - python

I am doing WebScraping on the Frankfurter Allgemeine Zeitung Archiv and I need to count how many times the word 'Bürokratie' appears in their articles.
From 1st October 2018 to 31st October 2018 the word appeared 75 times.
I have the string "75 Treffer". How can I extract the number 75 from that string using selenium?

This code extract number any way from the string.
# Python3 code to demonstrate
# getting numbers from string
# using List comprehension + isdigit() +split()
# initializing string
test_string = "There are 2 Cars for 8 persons"
# printing original string
print("The original string : " + test_string)
# using List comprehension + isdigit() +split()
# getting numbers from string
res = [int(i) for i in test_string.split() if i.isdigit()]
# print result
print("The numbers list is : " + str(res))
Hope this will help you to address your issue.

Call the split function using " " as the place to split at then cast to an int:
yourString = "75 whatever"
splitString = yourString.split( ' ' )
yourInt = int( splitString[0] )

Related

How do i add spaces in a string that has been reversed?

So basically, i am trying to get the user to input something like "123" and recieve the output "3 2 1"
but i cant figure out how to add the spaces
# current code
number = str(input("Type out a number with more then 1 charachter: "))
print("Original number:", number)
print("Number in reverse:", number[::-1])
I apologize in advance, im really new to programming.
Use str.join:
print("Number in reverse:", ' '.join(number[::-1]))
Or use an iterator reversed:
print("Number in reverse:", ' '.join(reversed(number)))
Use str.join:
print("Number in reverse:", " ".join(number[::-1]))
You can use str.join:
print("Number in reverse:", ' '.join(number[::-1]))
Here a space or ' ' is added between the characters.
The join() method returns a string created by joining the elements of
an iterable by string separator. exmaples for iterable objects are
strings and lists.

How to edit a string to add a variable at a particular place in python?

I have a variable:
a = 2
and I have this string:
"//media.boohooman.com/i/boohooman/mzz11035_black_xl/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"
I want edit this string so I can add variable, a, at a specific place in this string, after "mzz11035_black_xl", to make the whole string look like:
"//media.boohooman.com/i/boohooman/mzz11035_black_xl_2/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"
What options do I have to achieve this goal. I know that there are some hard coded ways where I can count the characters before and after a specific place and do slicing, but I am looking for some more general method so that it would work even if the strings change a bit. eg.
"//media.boohooman.com/i/boohooman/mzz11035_blue_xl/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"
or
"//media.boohooman.com/i/boohooman/mzz11035_blue_s/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"
Thanks.
You need to use f string or the format() function:
var = "_2"
f"//media.boohooman.com/i/boohooman/mzz11035_black_xl{var}/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"
or
var = "_2"
"//media.boohooman.com/i/boohooman/mzz11035_black_xl{}/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp".format(var)
if the position of the "mzz11035_black_xl" is changing you can do this:
var = "_2"
split_with = "mzz11035_black_xl"
initial_string = "//media.boohooman.com/i/boohooman/mzz11035_black_xl{var}/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"
# split the string into two parts
split_string = initial_string.split(split_with)
# Add the parts back with any sting in between
resulting_string = split_string[0] + split_with + var + split_string[1]
but in this case, you need to make sure that you have only one "mzz11035_black_xl" in your string.
If the string is changing but the link structure doesn't change you can try splitting with "/" (not elegant now but can be optimized)
var = "_2/"
split_with = "/"
initial_string = "//media.boohooman.com/i/boohooman/mzz11035_blue_s/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"
# initializing K
K = 6
# printing original string
print("The original string is : " + str(initial_string))
# Using split() + join()
# Split string on Kth Occurrence of Character
temp = initial_string.split(split_with)
resulting_tuple = split_with.join(temp[:K]), split_with.join(temp[K:])
# Convert to list and insert any string
resulting_list = list(resulting_tuple)
resulting_list.insert(1,var)
# convert to string
resulting_string = ""
resulting_string = resulting_string.join(list(resulting_list))
print("Is list after Kth split is: " + resulting_string)
Output:
"Is list after Kth split is : //media.boohooman.com/i/boohooman/mzz11035_blue_s_2/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"
you can use replace() function.
Syntax:
string.replace(old, new, count)
Parameters:
old – old substring you want to replace.
new – new substring which would replace the old substring.
count – the number of times you want to replace the old substring with the new substring. (Optional)
Return Value:
It returns a copy of the string where all occurrences of a substring are replaced with another substring.
Code sample:
a = "2"
str_to_replace = "//media.boohooman.com/i/boohooman/mzz11035_black_xl/mens-black-man-signature-embroidered-t-shirt?$product_page_main_magic_zoom$&fmt=webp"
old = "mzz11035_black_xl"
new = "mzz11035_black_xl_{}".format(a)
str_replaced = str_to_replace.replace(old, new)
print(str_replaced)

How to return an ordered string from an unordered one?

I am trying to solve a problem but struggling with the logic approach to it. The problem is as follows:
"Your task is to sort a given string. Each word in the string will contain a single number. This number is the position the word should have in the result.
Note: Numbers can be from 1 to 9. So 1 will be the first word (not 0).
If the input string is empty, return an empty string. The words in the input String will only contain valid consecutive numbers."
For e.g. "is2 Thi1s T4est 3a" --> "Thi1s is2 3a T4est"
I have tried the following so far:
def order(sentence):
my_string = "is2 Thi1s T4est 3a"
new_string = " "
for i in my_string:
if i in my_string == none
return new_string = " "
else:
if i in my_string
return new_string
But stuck on continuing the next bit. How can I put "put words into order starting from 1" into python code into my for loop?
I'm a beginner in python and programming so I am not entirely sure if the approach I'm making is the best logical way to do so, by creating an empty string new_string and then sorting my_string into that. Is this a good way of approaching this? I am stuck on which direction to go after this.
you could do :
r = "is2 Thi1s T4est 3a"
def get_number(w) :
for x in w :
try :
i = int(x)
return i
except :
pass
return None
ordered_list = " ".join((sorted( s.split(' ') , key = get_number)))
the function get_number allows to get the first number appearing in a word, so we split the sentence to get the words it's made of , r.split(' ') gives ['is2', 'Thi1s', 'T4est', '3a'], we order then the list using ordered builtin function , it takes a list and a function that yields the keys over which we want to order the list, and outputs the ordered list, we then use the builtin function join to join the list using a space separator.
Output :
Out[425]: 'Thi1s is2 3a T4est'

python string manipulation to match windows firewall syntax

I have a string like so:
string = "27.116.56.0 27.116.59.255 43.230.209.0 43.230.209.255" #(white space sep)
how would you go from it to this format:
string = "27.116.56.0-27.116.59.255,43.230.209.0-43.230.209.255"
**the string will have unknown length , the elements number will allways be even.
I've looked at some close examples and got confused..
what is the best easy way doing that?
# Create a new list containing the ips
str_elems = "27.116.56.0 27.116.59.255 43.230.209.0 43.230.209.255".split()
# Use a format string to build the new representation, where each list element is assigned a spot in the string
# We use the * operator to convert the single list into multiple arguments for the format
new_str = ("{}-{},"*(len(str_elems)/2)).format(*str_elems).rstrip(',')
For a general solution, you can iterate over your split string in chunks of 2.
s = "27.116.56.0 27.116.59.255 43.230.209.0 43.230.209.255".split()
print(",".join(["-".join(s[i:i + 2]) for i in range(0, len(s), 2)]))
#'27.116.56.0-27.116.59.255,43.230.209.0-43.230.209.255'
Join the inner chunks with a "-" and finally join the whole thing with ","
string = "27.116.56.0 27.116.59.255 43.230.209.0 43.230.209.255"
ip_list = string.split(" ") # split the string to a list using space seperator
for i in range(len(ip_list)): # len(ip_list) returns the number of items in the list (4)
# range(4) resolved to 0, 1, 2, 3
if (i % 2 == 0): ip_list[i] += "-" # if i is even number - concatenate hyphen to the current IP string
else: ip_list[i] += "," # otherwize concatenate comma
print("".join(ip_list)[:-1]) # "".join(ip_list) - join the list back to a string
# [:-1] trims the last character of the result (the extra comma)

Python: Remove First Character of each Word in String

I am trying to figure out how to remove the first character of a words in a string.
My program reads in a string.
Suppose the input is :
this is demo
My intention is to remove the first character of each word of the string, that is
tid, leaving his s emo.
I have tried
Using a for loop and traversing the string
Checking for space in the string using isspace() function.
Storing the index of the letter which is encountered after the
space, i = char + 1, where char is the index of space.
Then, trying to remove the empty space using str_replaced = str[i:].
But it removed the entire string except the last one.
List comprehensions is your friend. This is the most basic version, in just one line
str = "this is demo";
print " ".join([x[1:] for x in str.split(" ")]);
output:
his s emo
In case the input string can have not only spaces, but also newlines or tabs, I'd use regex.
In [1]: inp = '''Suppose we have a
...: multiline input...'''
In [2]: import re
In [3]: print re.sub(r'(?<=\b)\w', '', inp)
uppose e ave
ultiline nput...
You can simply using python comprehension
str = 'this is demo'
mstr = ' '.join([s[1:] for s in str.split(' ')])
then mstr variable will contains these values 'his s emo'
This method is a bit long, but easy to understand. The flag variable stores if the character is a space. If it is, the next letter must be removed
s = "alpha beta charlie"
t = ""
flag = 0
for x in range(1,len(s)):
if(flag==0):
t+=s[x]
else:
flag = 0
if(s[x]==" "):
flag = 1
print(t)
output
lpha eta harlie

Categories

Resources