Skip to content

Tag: string

Python split function. Too many values to unpack error

I have a python function that must read data from file and split it into two key and value, and then store it in dictionary. Example: file: I use the split function for it, but when there is really a lot of data it raises value error What can I do about this ? This is the exact code that

Check if a string contains a number

Most of the questions I’ve found are biased on the fact they’re looking for letters in their numbers, whereas I’m looking for numbers in what I’d like to be a numberless string. I need to enter a string and check to see if it contains any numbers and if it does reject it. The function …

How to check if a string is a valid regex in Python?

In Java, I could use the following function to check if a string is a valid regex (source): Is there a Python equivalent of the Pattern.compile() and PatternSyntaxException? If so, what is it? Answer Similar to Java. Use re.error exception: exception re.error Exception raised when a string passed to one of th…

Convert set to string and vice versa

Set to string. Obvious: String to set? Maybe like this? Extremely ugly. Is there better way to serialize/deserialize sets? Answer Use repr and eval: Note that eval is not safe if the source of string is unknown, prefer ast.literal_eval for safer conversion: help on repr: