Skip to content
Advertisement

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

How does the max() function work on list of strings in python?

I have a list: It gives: Please also explain how it perform comparison on list of strings and list of numbers. Answer This is actually a good question and the answer varies depending on whether you’re on python2.x or python3.x … And which python implementation you’re using1. See here for a description of how python compares different types. The link

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 isdigit() only returns True if

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 the functions here is not a valid regular expression

How to split the integers and Operators characters from string in python?

I want to split the string into integers and operators for doing Infix expression evaluation in python. Here is my string: I tried this to split: This is wrong. Since ’10’ is splitted into ‘1’,’0′ I tried alternative: This is also went wrong. Since ‘)*’ should be splitted into ‘)’, ‘*’ Could you help to split the operators and integers

format strings and named arguments in Python

Case 1: It will give KeyError: ‘arg1′ because I didn’t pass the named arguments. Case 2: Now it will work properly because I passed the named arguments. And it prints ’10 20’ Case 3: And, If I pass wrong name it will show KeyError: ‘arg1’ But, Case 4: If I pass the named arguments in wrong order It works… and

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:

Advertisement