Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I Have string, from some xml element:
source = "<![CDATA[<p><span stylefontfamily times new romantimesserif fontsize large>Is important ?</span></p>]]
"
I wanna Remove specified string base on this list:
mustDelWord=["<p>","</p>","<span stylefontfamily times new romantimesserif fontsize large>","</span>"]
So, the expected output is:
<![CDATA[Is important ?]]>
So far code is :
mustDelWord=["<p>","</p>","<span stylefontfamily times new romantimesserif fontsize large>","</span>"]
for i in mustDelWord:
source = source.replace(mustDelWord[i],"")
print source
But occur this error:
source = source.replace(mustDelWord[i],"")
TypeError: list indices must be integers, not str
Thanks.
Just change the line to:
source = source.replace(i,"")
This is because the loop for i in mustDelWord already iterates over the elements in the list, not the indices in that list, so you don't have to do mustDelWord[i].
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 months ago.
Improve this question
I have a text file containing bits so it’s like „1000101011010110000…“ in this text. I want python to interpret this text as bytes and perform different byte transformations with it. But how do I red it on as bytes without python thinking it’s a string?
The built-in int function has an parameter base for specifying the base.
To convert a string into an integer with base 2 (binary), pass 2 into it:
s = input()
num = int(s, 2)
# Manipulate `num` as you like
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
How would I replace characters in a string for certain indices in Python?
For example, I have version = "00.00.00" and need to change each of the 0s to a different value, say 3, to look like "33.33.33". Also, would this be possible if I had a variable storing this value. If I have vnumber = "3", would I be able to get the same output by using the variable? I'm sure replace() is a good function to use for this, but I'm not sure about syntax.
From an interactive session, you could type:
>>> help(str.replace)
But to answer the question most directly:
vnumber = '3'
newversion = version.replace('0', vnumber)
Is probably what you want to do.
Your guess about str.replace was right. It takes to arguments, the first is the string to be found in the original string, and the second is the string to replace the found occurrences of the first argument with. Code could be like this:
vnumber = "3"
version = "00.00.00"
newversion = version.replace("0", vnumber)
print(newversion)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Python move array to another one and add quotation mark to it. Code below
cars = ["ab","bc","ca"]
people = ['"ab"','"bc"','"ca"']
such as like that:
array cars move to people and add quotation
Welcome to Stack Overflow. You can format the string with quotaiton mark and then append it to an array. e.g.
cars = ["ab","bc","ca"]
people = []
for elem in cars:
new_elem = '"{}"'.format(elem)
people.append(new_elem)
print(people)
Output:
['"ab"', '"bc"', '"ca"']
You can use a list comp to do it in one line:
people=['"%s"' % i for i in cars]
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
The string i needed to format is,
string = "How are you? abcdef"
I need to remove the "abcdef" from the string.
string = string.split(' ')[0]
Edit: Explanation. The line of code above will split the single string into a list of strings wherever there is a double space. It is important to note that whatever is split upon, will be removed. [0] then retrieves the first element in this newly formed list.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a list of data that reads
[['name','emailtype','phonetype'],['john','yahoomail', 'mobile'],['mark','yahoo','landline']]
I can manually pick out the values i.e print dL[0][0] prints name and dL[1][0] emailtype.
Is it possible to isolate all the names from the list. i.e john and mark. With a program / module and then print them
and produce them into something like this:
1) John
2) Mark
so that I can ask for a raw_input and then if I press 1 as selection it produces john as the answer.
so that it reads similar to the nicely written data that I can manually type as above.
You want to slice the list (to ignore the first row), then use a list comprehension to pick out just the first element of each nested list:
[row[0] for row in nested_list[1:]]