How to get rid of whitespace in element of dictionary? - python

I am trying to extract only the string without whitespace of an element in a dictionary. This is how my element looks currently:
u'GYM-7874 '
I only want to extract the value without the whitespace so "GYM-7874" only so that I can compare it with another list of strings. I tried using the .strip() method to get rid of whitespaces but unfortunately that only works with string. How do I get rid of the white space and only extract the characters?
# json_ob is the string array of dictionary
title = json_ob[index]["title"]
title.strip()

strip() doesn't modify the string in place (strings are immutable in Python). You need to assign the result back to the variable.
title = title.strip()

Related

Python 3 slice string from a last certain character

I am trying to slice URLs from the last symbol "/".
For example, I have an URL http://google.com/images/54152352.
Now I need the part of that image which is 54152352.
I understand that I could simply slice it with slicing from a certain character, but I have a list of URLs and each of them is different.
Other examples of URLs:
https://google.uk/images/kfakp3ok2 #I would need kfakp3ok2
bing.com/img/3525236236 #I would need 3525236236
wwww.google.com/img/1osdkg23 #I would need 1osdkg23
Is there a way to slice the characters from the last character "/" in a string in Python3? Each part from a different URL has a different length.
All the help will be appreciated.
target=url.split("/")[-1]
split methode returns a list of words separated by the separator specified in the argument
and [-1] is for the last element of that list
You can use the rsplit() functionality.
Syntax:
string.rsplit(separator, maxsplit)
Reference
https://www.w3schools.com/python/ref_string_rsplit.asp
rsplit() splits the string from the right using the delimiter/separator and using maxsplit you can split only once with some performance benefit as compared to split() as you dont need to split more than once.
>>>> url='https://google.uk/images/kfakp3ok2'
>>>>
>>>> url.rsplit('/', 1)[-1]
'kfakp3ok2'
>>>>

Problems when trying to filter strings in a list?

I have a large list with strings and I would like to filter everything inside a parenthesis, thus I am using the following regex:
text_list = [' 1__(this_is_a_string) 74_string__(anotherString_with_underscores) question__(stringWithAlot_of_underscores) 1.0__(another_withUnderscores) 23:59:59__(get_arguments_end) 2018-05-13 00:00:00__(get_arguments_start)']
import re
r = re.compile('\([^)]*\)')
a_lis = list(filter(r.search, text_list))
print(a_lis)
I test my regex here, and is working. However, when I apply the above regex I end up with an empty list:
[]
Any idea of how to filter all the tokens inside parenthesis from a list?
Your regex is OK (though perhaps you don't want to capture the parentheses as part of the match), but search() is the wrong method to use. You want findall() to get the text of all the matches, rather than the indices of the first match:
list(map(r.findall, text_list))
This will give you a list of lists, where each inner list contains the strings which were inside parentheses.
For example, given this input:
text_list = ['asdf (qwe) asdf (gdfd)', 'xx', 'gdfw(rgf)']
The result is:
[['(qwe)', '(gdfd)'], [], ['(rgf)']]
If you want to exclude the parentheses, change the regex slightly:
'\(([^)]*)\)'
The unescaped parentheses within the escaped ones indicate what to capture.

Format string in python list

I have a list which should contain string with a particular format or character i.e. {{str(x), str(y)},{str(x), str(y)}}. I tried to do string concat like: "{{"+str(x), str(y)+"},{"+str(x), str(y)+"}}" and append to list, but it gets surrounded by brackets: [({{str(x), str(y)}),({str(x), str(y)}})]
How can I get rid of the brackets or betterstill, is there a better approach to having a list without brackets like this: [{{string, string},{string, string}}]
The parentheses are because you're creating a tuple of three items:
"{{"+str(x)
str(y)+"},{"+str(x)
str(y)+"}}"
Try replacing those bare commas between str(x) and str(y) with +","+:
"{{"+str(x)+","+str(y)+"},{"+str(x)+","str(y)+"}}"

How to remove brackets from python string?

I know from the title you might think that this is a duplicate but it's not.
for id,row in enumerate(rows):
columns = row.findall("td")
teamName = columns[0].find("a").text, # Lag
playedGames = columns[1].text, # S
wins = columns[2].text,
draw = columns[3].text,
lost = columns[4].text,
dif = columns[6].text, # GM-IM
points = columns[7].text, # P - last column
dict[divisionName].update({id :{"teamName":teamName, "playedGames":playedGames, "wins":wins, "draw":draw, "lost":lost, "dif":dif, "points":points }})
This is how my Python code looks like. Most of the code is removed but essentially i am extracting some information from a website. And i am saving the information as a dictionary. When i print the dictionary every value has a bracket around them ["blbal"] which causes trouble in my Iphone application. I know that i can convert the variables to strings but i want to know if there is a way to get the information DIRECTLY as a string.
That looks like you have a string inside a list:
["blbal"]
To get the string just index l = ["blbal"] print(l[0]) -> "blbal".
If it is a string use str.strip '["blbal"]'.strip("[]") or slicing '["blbal"]'[1:-1] if they are always present.
you can also you replace to just replace the text/symbol that you don't want with the empty string.
text = ["blbal","test"]
strippedText = str(text).replace('[','').replace(']','').replace('\'','').replace('\"','')
print(strippedText)
import re
text = "some (string) [another string] in brackets"
re.sub("\(.*?\)", "", text)
# some in brackets
# works for () and will work for [] if you replace () with [].
The \(.*?\) format matches brackets with some text in them with an unspecified length. And the \[.*?\] format matches also but a square brackets with some text inside the brackets.
The output will not contain brackets and texts inside of them.
If you want to match only square brackets replace square brackets with the bracket of choice and vise versa.
To match () and [] bracket in one go, use this format (\(.*?\)|\[.*?\]:) joining two pattern with the | character.

Remove substrings of variable length from string

I have a list of strings where all of the strings roughly follow the format 'foo\tbar\tfoo\n' in that there are three segments of variable length that are separated by two tabs (\t) and with a newline indicator at the end (\n).
I want to remove everything except for the text before the first \, so that it would return as 'foo'. Given that the first segment is of variable length, I'm not sure how I can do that.
Use str.split():
>>> string = 'foo\tbar\tfoo\n'
>>> string.split('\t', 1)[0]
'foo'
This splits the string by the first occurrence of the '\t' tab character, which returns a list with two elements. The [0] selects the first element in the list, which is the part of the string before the first '\t' occurrence.
Just search for the first \t character, and get everything before it. Slicing makes this easy.
newstr = oldstr[:oldstr.find("\t")]
Try with:
t = 'foo\tbar\tfoo\n'
t[:t.index("\t")]

Categories