How to split with multiple bracket [closed] - python

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 8 years ago.
Improve this question
So I want to do something like this
"(1.0)" which returns ["1","0"]
similarly "((1.0).1)" which returns ["(1.0)", "1")
How do i do this python? Thanks for the help
so basically I want to break the string "(1.0)" into a list [1,0] where the dot is the separator.
some examples
((1.0).(2.0)) -> [(1.0), (2.0)]
(((1.0).(2.0)).1) -> [((1.0).(2.0)), 1]
I hope this is more clear.

Here is my version:
def countPar(s):
s=s[1:-1]
openPar=0
for (i,c) in enumerate(s):
if c=="(":
openPar+=1
elif c==")":
openPar-=1
if openPar==0:
break
return [s[0:i+1],s[i+2:]]

You'll need to build a little parser. Iterate through the characters of the string, keeping track of the current nesting level of parentheses. Then you can detect the . you care about by checking that first, the character is a ., and second, there's only one level of parentheses open at that point. Then just place the characters in one buffer or another depending on whether you've reached that . or not.

Related

Variable with " " in Python [closed]

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 months ago.
Improve this question
Trying to make a for loop work in Python I came across a variable declared with "".
i.e: res=""
What is the purpose of the quotes?
Want to know what happens in there.
res="" is an empty string. This can be used later to, for example:
Add another string to it: res += "ABC"
Check whether there is a result. An empty string returns False, while a string with at least 1 character returns True.
Check the length of the string by len(res).
I could go on but you get the point. It's a placeholder.

How do to get python to not calculate number variable with dash? [closed]

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
I need to pass a variable '3-123' to a method in python, but if I do str(3-123) I get '-120'. Tried iterating, but I got an error cause it's an int.
You simply pass the string "3-123".
Your expression str(3-123) tells Python to first evaluate what is in parentheses, which is very clearly the arithmetic expression 3-123. That evaluation mandates a subtraction.
UPDATE PER USER COMMENT
Since you just got it returned from REST, then it's already a string. It seems that your problem is that you're building an expression string to be evaluated in SQL. In this case, you need to build the string you're going to send to SQL, at the character level. For this one item you would extend your 3-123 string with quotation marks:
from_rest = "3-123" # In practice, this comes directly from your REST return value.
to_sql = '"' + from_rest + '"'
This leaves you with a variable that contains the string "3-123" -- seven characters, rather than the original five.
Is that what you needed?

i want to write a python program that converts an integer to its corresponding month [closed]

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
I want to add a error message if it exceeds 12 or if a character is entered without any if statements. here is what I have written so far:
import calender
def get_mont_name(month_number):
try:
return calender.month_name[month_number]
except IndexError:
print("'[]' is not a valid month number".format(month_number))
Your code is really close to working!
Firstly, you've spelt calendar wrong, but I assume this is just a typo.
You've slightly misused format() which is probably causing your issue. Replacing your print statement with the following should fix it:
print("{num} is not a valid month number".format(num = month_number))
The curly brackets allow the format() method to identify the parts of the string you'd like to modify.
I hope this helps!

Regex- match on a specific string with any two digit integer in it? [closed]

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
I have a list of filenames that look like this:
red.t<0 padded int>z.white.blue<0 padded int>.ab00.txt2
For example:
red.t01z.white.blue12.ab00.txt2
red.t02z.white.blue45.ab00.txt2
red.t03z.white.blue09.ab00.txt2
I want to match on this sequence, for any two digit number. The 00 near the end is constant, and it shouldn't match on any other values there. ie, this wouldn't match red.t03z.white.blue09.ab01.txt2.
I tried red.t[0-9]*z.white.blue[0-9]*.ab00.txt, but that only works when I have the first [0-9]* in there, the second one makes it no longer match. What is the solution to this?
You could use anchors to assert the start and end of the string, escape the dot to match it literally and use a quantifier 0-9[{2} to match 2 digits.
^red\.t[0-9]{2}z\.white\.blue[0-9]{2}\.ab00\.txt2$
Regex demo

Delete the first word and everything after third comma - Python 2.7 [closed]

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 8 years ago.
Improve this question
Say I have a line like this:
aaaaaaa, bbbbbbb, ccccccc, ddddddd
And I need to cut it to look like this:
bbbbbbb, ccccccc
So how to delete the first word and everything after third comma ?
Here's a couple of tricks you could use to achieve what you want:
Use .split(', ') to break your line into an array of words
Use the sub-array notation ([1:3] for example) to keep the second and third words
Reconstruct the array back into a line using .join, supplying any delimiter you'd like (e.g. a new comma)
For example:
', '.join("aaa, bbb, ccc, dddd, eeee".split(', ')[1:3])
def wordsTwoAndThree( csvString, sep ):
return sep.join(csvString.split(sep)[1:3])
print( wordsTwoAndThree("aaaaaaa, bbbbbbb, ccccccc, ddddddd", ',') )
Similarly to #Paedolos 's suggestion.

Categories