Python converting a list of strings to unicode [duplicate] - python

This question already has answers here:
How to detect string byte encoding?
(4 answers)
What is the difference between encode/decode?
(7 answers)
Closed last month.
I have a list of strings that originally was a list of unicode element. And so, some string contains some character accent in unicode formate.
list=['Citt\xe0','Veszpr\xe9m','Publicit\xe0']
I need to get a new list that looks like this:
new_list=[u'Citt\xe0',u'Veszpr\xe9m',u'Publicit\xe0']
Each element of the new_list has to carry both the u and the accent.
Is there a way to do it iterating on each element?

new_list=[unicode(repr(word)) for word in old_list]
>>> print new_list
[u"'Citt\\xe0'", u"'Hello'", u"'Publicit\\xe0'"]
Is that what you want?

Related

Matching of element in List [duplicate]

This question already has answers here:
Check if multiple strings exist in another string
(17 answers)
Closed last month.
How can I check a string for substrings contained in a list, like in Check if a string contains an element from a list (of strings), but in Python?
Try this test:
any(substring in string for substring in substring_list)
It will return True if any of the substrings in substring_list is contained in string.
Note that there is a Python analogue of Marc Gravell's answer in the linked question:
from itertools import imap
any(imap(string.__contains__, substring_list))
In Python 3, you can use map directly instead:
any(map(string.__contains__, substring_list))
Probably the above version using a generator expression is more clear though.

How to split string before a character then have python create a list from it [duplicate]

This question already has answers here:
How can I split and parse a string in Python? [duplicate]
(3 answers)
Closed 1 year ago.
I am trying to make python take a string, separate the characters before another character eg: "10001001010Q1002000293Q100292Q". I want to separate the string before each Q and have python create either a list or another string. I cannot figure this out for the life of me.
You can do this using the split function, give "Q" as a parameter to the split function then you can slice the list to only get the numbers before Q.
num = "10001001010Q1002000293Q100292Q"
print(num.split("Q")[:-1])
Split() function: https://www.w3schools.com/python/ref_string_split.asp
Slicing: https://www.w3schools.com/python/python_strings_slicing.asp
The syntax is str.split("separator").
str = str.split("Q")
Then output will be ['10001001010', '1002000293', '100292', ''].
If you don't need the last empty element then you can write as:
str = str.split("Q")[:-1]

How to convert string without delimiter to list in python [duplicate]

This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 3 years ago.
So I have string like this:
"UFFKTEWKW"
And I need to convert it to a list or tuple like this:
("UFF", "KTE", "WKW")
So every 3 letters from the string goes to separate element of list or tuple.
I can't use split() here because string doesn't have any delimiters. I don't wanna make some dummy for cycle for it. And I think there should be simple solution for it.
You can use this.
a="UFFKTEWKW"
out=tuple(a[i:i+3] for i in range(0,len(a),3))
# ('UFF', 'KTE', 'WKW')

Removing Python characters from a list [duplicate]

This question already has answers here:
Slice every string in list in Python
(3 answers)
What is the purpose of the two colons in this Python string-slicing statement?
(1 answer)
Closed 4 years ago.
If i had a list as an example:
a = ['Hello_1.txt', 'Hello_2.txt']
In Python is it possible to somehow remove the first 5 characters ('Hello')
and the last 3 characters ('txt') from each of the items in the list ?
You could use a list-comprehension and string slicing:
[s[5:-3] for s in a]
which gives what you describe (not sure this is the neatest output though!)
['_1.', '_2.']

Convert string of list to python list [duplicate]

This question already has answers here:
Converting a string to a tuple in python
(3 answers)
Closed 8 years ago.
I downloaded a csv table from a database using SQL. One of the fields has values like so:
'[0.1234,0.0,0.0]'
I want to convert this string to a python list to get the first value. I only know how to convert strings to ints and floats... is there any way to de-string this object? The table I got from SQL is from a web-based viewer, I'm not getting it from my command line.
You could take the substring from index 1 to index -1 and then split it using the comma as a delimiter. In python
array = variable[1:-1].split(',')
should work.
If you're sure it is always valid list syntax, you could use"
myList = eval('[0.1234,0.0,0.0]')
Or if the value itself has quotes ' in it, you can slice those off
value = "'[0.1234,0.0,0.0]'"
myList = eval(value[1:-1])
Then to get the first value you just
myList[0]

Categories

Resources