How to parse string with comma in Python? - python
I am having problems parsing a string to a function in python.
def logAppend(self, data):
print(data)
When I parse a string with a comma in the above code it returns the following.
TypeError: logAppend() takes exactly 2 arguments (3 given)
I am kinda new to Python, so please take it easy on me if I'm missing something simple here..
It's unclear what your string is, but if you're assuming it's (self, data), those two things are actually the variables for your function. Either could be calling a string, or more likely a list of strings, that are would be ['string1', 'string2'], for example.
Related
Why can I not add an 'int' to 'str' in print function by using '+'? [duplicate]
This question already has answers here: Why does Python not perform type conversion when concatenating strings? (8 answers) Closed 6 months ago. print ("hello"+"world) or print ("hello" +"world") would give the output as: hello world however, when I try to insert a variable (int) into the print functions along with 'str' using '+', there's an error. But, when I use ','s instead, it is designed to work. That is: x=1 print("the value of 'x' is " + x + ".") it shows the error TypeError: can only concatenate str (not "int") to str But when I use: print("the value of 'x' is ",x,".") I get the desired output. I would like to know why is python designed this way, what is the meaning of using '+' in this context. Thankyou. Edit: Thanks for the replies, the intent was because in Java + means to simply put together.
Treating this operation as “+ in print function” is the wrong mental model. There are two entirely separate operations here: print which knows it needs to derive a string to display it, + which knows nothing about the followup usage of its result. Functions (and by extension methods and operators) in Python are not return type polymorphic, which is a fancy way of saying functions do not know what output type is expected from them. Just because + is used to derive the argument to print does not tell it that a string is the desired result. The operation :str + :int has no well defined output type. What is "1" + 1? Is it 2, is it "11", is it 11 or something else? There is no clear answer for these two types and Python thus consistently raises TypeError. Whether or not the result is eventually used in print is irrelevant. In contrast, print serves to write a string-representation of its arguments to a stream. Every argument must eventually result in a string and be written to the stream. The operation print(:str, :int) does not need to concatenate a str and int - it can separately convert each to str and write it immediately.
print() is just a function and it was designed in a way that it accepts variable number of positional arguments and it internally convert them to str. As you can see here at it's documentation: print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False): . . . All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end. . . . When you do print("the value of 'x' is ",x,".") you are passing 3 distinct arguments to the function which the function gets as objects. And they are converted to string as mentioned above. However, in the line print("the value of 'x' is " + x + ".") you're trying to concatenate the values and pass as a SINGLE argument and since the concatenation operation isn't a valid operation (python doesn't have coercion like javascript does to convert unlike types amid operations), it fails before it ever reaches the print function.
Create a List of given Numbers in Python
I would like to create an array of Integers like this (15,0,15,47,0,15,15,0,0,15,0,0,17,0,14,0,0,15,0,0,22,29,0,0,29,22,15,15,0,15,15,0,16,0,0,16,0,0,0,0,17,0,0,19,21,0,17,16,15,0,16,0,0,15,0,16,0,0,0,15,0,16,16,0,0,0,14,21,14,21,14,0,14,29,0,14,15,15,16,0,0,0,29,22,0,0,0,0,14,0,0,0,15,0,15,16,0,0,31,14,0,0,0,0,13,13,0,0,0,14,20,27,0,0,0,0,0,15,29,15,0,0,0,0,21,28,0,15,15,16,0,0,0,0,0,15,0,0,0,15,0,15,0,0,0,17,0,0,0,0,18,0,0,15,0,0,0,15,15,0,0,15,15,0,0,0,30,16,0,0,14,27,14,0,0,14,14,14,14,21,14,29,0,0,0,14,14,0,0,45,16,0,0,29,15,0,0,0,0,15,0,17,0,0,13,13,0,13,27,28,0,13,0,13,13,40,0,0,13,0,0,0,26,13,0,19,25,13,12,25,31,0,13,13,0,0,13,14,13,0,13,0,0,0,12,19,13,26,0,13,13,0,27,15,14,0,13,0,50,13,100) in Python.... I tried this: data = int(x,x,x,x,x,...etc) i have this error: TypeError: int() takes at most 2 arguments (254 given) How is it possible? I'm working on Jupyter with Python 3.7 thank you in advance!
The syntax you have in your question it is already a tuple, you can get a list using the list class for that effect. >>> tuple_numbers = (2,3,4,5,6) >>> list_numbers = list(tuple_numbers) >>> list_numbers [2,3,4,5,6] I see you're new to Python, at least seems to be that way. So I'll recommend reading about list and tuples. If you want to apply some transformation to list items, try map. Note Take into account that it is not the same: list(2,3,4,5) that list((2,3,4,5)). The first is wrong since you are passing more than one argument Happy coding!
If you enter help(int) on the Python interpreter: >>> help(int) you'll see it's documentation and a portion of it says: Convert a number or string to an integer ... Your question is not clear about where you are getting the numbers from. If you are getting them from a file, you'll have to: open the file read individual numbers. They will be strings. apply int() to each number string append the number to a list
Python error not enough arguments for format string
Can anyone tell me whats wrong with this: put(('%s%s.tar.gz' % config.SERVER_PROJECT_PATH, config.RELEASE)) TypeError: not enough arguments for format string I just want insert two variables in to the string, is my syntax correct?
You need to put the two values in a tuple: put('%s%s.tar.gz' % (config.SERVER_PROJECT_PATH, config.RELEASE)) otherwise Python sees this as two separate expressions to form a tuple, '%s%s.tar.gz' % config.SERVER_PROJECT_PATH and config.RELEASE.
The syntax is incorrect. The string formatting arguments must be a tuple. You are creating a tuple with the formatted string and the second formatting argument. Use this instead: put("%s%s.tar.gz" % (config.SERVER_PROJECT_PATH, config.RELEASE))
Converting a string-like object into a string in python
I am using python and XMLBuilder, a module I downloaded off the internet (pypi). It returns an object, that works like a string (I can do print(x)) but when I use file.write(x) it crashes and throws an error in the XMLBuilder module. I am just wondering how I can convert the object it returns into a string? I have confirmed that I am writing to the file correctly. I have already tried for example x = y although, as I thought, it just creates a pointer, and also x=x+" " put I still get an error. It also returns an string like object with "\n". Any help on the matter would be greatly appreciated.
file.write(str(x)) will likely work for you. Background information: Most types have a function __str__ or __repr__ (or both) defined. If you pass an object of such a type to print, it'll recognize that you did not pass a str and try to call one of these functions in order to convert the object to a string. However, not all functions are as smart as print and will fail if you pass them something that is not a string. Also string concatenation does not work with mixed types. To work with these functions you'll have to convert the non-string-type objects manually, by wrapping them with str(). So for example: x = str(x)+" " This will create a new string and assign it to the variable x, which held the object before (you lose that object now!).
The Library has __str__ defined: def __str__(self): return tostring(~self, self.__document()['encoding']) So you just need to use str(x): file.write(str(x))
I'm not quite sure what your question is, but print automatically calls str on all of it's arguments ... So if you want the same output as print to be put into your file, then myfile.write(str(whatever)) will put the same text in myfile that print (x) would have put into the file (minus a trailing newline that print puts in there).
When you write: print myObject The __repr__ method is actually called. So for example you could do: x += myXMLObject.__repr__() if you want to append the string representation of that object to your x variable.
Python repr function problem
I'm dealing with some text parsing in Python and for that purpose, it's good for me to apply repr() function on each string I'm gonna parse, but after the parsing, I need to convert some parsed substring back to the previous representation, because I want to print them and I'm not able to do this. I thought that str() function should get the string back to the human more readable form. But when I apply str function on that substring, nothing's changed. As I've said I need to print the string in human readable form, without printing escape sequences like \n, \t etc... But when I apply repr() to a string and then I want to convert it back, I don't know how, because str() function didn't do it. So my question is, how to convert the string back into human readable form? Thanks for every reply.
str() has no effect on objects that are already strings. You need to use eval() to undo a repr() where possible. Try using ast.literal_eval() instead though.