How to slice a unicode object? - python

I was trying to get a unicode string from my database and slice it to get part of it.
However I get:
TypeError: 'unicode' object is not callable
Code:
info = post.body(0)[0:50]
Here post.body(0) is a {unicode} variable.
I want the first fifty characters of it.

post.body is already a unicode object. Python is complaining that you are trying to call it with argument 0.
So, just try
info = post.body[0:50]

Related

Python '<' not supported between instances of 'str' and 'int' [duplicate]

I encountered the error
'>' not supported between instances of 'str' and 'int'
while trying to print the below lines in Pandas dataframe
print (survey_df_clean.shape)
print (survey_df_clean[survey_df_clean['text']>30].shape)
Should I try to convert them to int and how would that work in this statement?
First make sure that all value of survey_df_clean['text'] is the same, if you want to convert as numeric, do this :
survey_df_clean['text'] = pd.to_numeric(survey_df_clean['text'])
Then do this
survey_df_clean.loc[survey_df_clean['text']>30].shape
This message suggests, that you try to compare a string object (str) with an integer (int).
The expression
survey_df_clean['text']
will probably return a string. Therefore, you cannot directly compare it with the number 30. If you want to compare the length of the entry, you can use the pandas.Series.str.len() operation as you can see here.
If this field should actuallty contain an integer, you can use this method (pandas.to_numeric) to cast it from str to int.
survey_df_clean['text'] might have NAN or str values in it some where.
to find out :
survey_df_clean['text'].isnull().sum()
if they are,first take care of them then apply
print (survey_df_clean[survey_df_clean['text']>30].shape)
I had the same error message when trying to use that conditional. What intrigued me was that the same command had run correctly on another notebook.
The difference was in how I read the csv file. This was the troublesome one:
df=pd.read_csv('data.csv')
And when I put the decimal argument it worked:
df=pd.read_csv('data.csv', decimal=',')
Obviously, it'll depend on how your data is organized. ;)
This is because values in 'text' column are of type str and you are comparing str with int.
You can do a quick check for getting type of 'text' column.
print(type(survey_df_clean['text'][:1][0]))
For comparing you can do as following
survey_df_clean[survey_df_clean['text'].astype(int)>30]

Getting AttributeError: 'int' object has no attribute 'isnumeric' error

Here I'am trying to build a simple calculator using tkinter and I have used some number images as buttons,i want only numbers and mathematical characters to be entered in entry box,but when i press the number button i get AttributeError: 'int' object has no attribute 'isnumeric' error, I didn't get the solution for this problem:
here is my code and below code is function for tkinter button:
def press(n):
new=value.get()
if new=="Can't divide by zero" or new=="Can't perform operation":
new=''
if n.isnumeric() or n=='+' or n=='-' or n=='*' or n=='/' or n=='%' or n=='.':
new+=str(n)
value.set(new)
The python isnumeric() method expects a string and checks if the characters in the string are numeric. If you're already passing n into def press(n) as an integer there is no reason to check if it's numeric and it's expecting a string which is why you get the AttributeError: 'int' object has no attribute 'isnumeric'. Your input should be a string, not an int literal.
The python isnumeric() method expects a string and checks if the characters in the string are numeric
as bpiekars said, and you can try:
str(n).isnumeric()

String object has no attribute 'decode' when converting UTF-8

I'm trying to convert G\xc3\xb6del to Gödel (specifically, \xc3\xb6d to ö), but I can't find a method for going about doing this. When I run the below code, I receive an error:
>>> string = '\xc3\xb6'
>>> string.decode(encoding='UTF-8')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'decode'
This question didn't seem to help, nor did any others that seemed similar, as they were all from 2.x. A friend mentioned base 64 encoding, but I'm not sure in what way that helps. I can't seem to find what I'm supposed to do to convert it in 3.8, so what would be the best way to go about doing this?
The issue here is that a string is already decoded. Basically you encode a string object to a byte object, and the inverse operation is decoding a byte object to a string object. That's why a string has no attribute decode. Think of it like this:
String -> encode -> Byte
Byte -> decode -> String
In this case, the solution would be to call the encode method and pass in 'utf8' or 'ascii', depending on the context and situation.
However, it isn't just converting it to a string object that is the case here. As the OA of this question, I do know exactly what this was meant for, and how I came to a solution. The value Gödel was gained by scraping an SCP Foundation page, finding the Object Class to then pass onto my Discord bot for a command. Here was my code:
link = f"http://www.scp-wiki.net/scp-{num}"
page = get(link)
obj_class = [str(i) for i in page.iter_lines() if b"Object Class:" in i][0]
# ^ There should only be one line in the document matching that requirement.
# The type of this line is a byte object, which is why conversion is necessary later on.
obj_class = re.findall('(?<=\<\/strong> )(.*?)(?=\<)', obj_class)[0]
# ^ Find the actual class in that line.
print(obj_class) # expected Gödel, got G\xc3\xb6del instead.
The above would not raise an exception, it just simply wouldn't convert the character encoding as desired. My fix was simple, once I understood what was going on; replace the str(i) for i.decode('utf8').
obj_class = [i.decode('utf8') for i in page.iter_lines() if b"Object Class:" in i][0]
# ^ decoding it there really makes the difference, converting it to utf-8 without dealing with
# the issues of decoded strings later on.
This would now return the desired value, Gödel, rather than G\xc3\xb6del. I hope that this helps. Please let me know if I've made any mistakes, so I can make any necessary corrections.

How to solve this TypeError issue in python3? "TypeError: a bytes-like object is required, not 'str'"

Recently I switched from python2.7 to python3.7.3 In my project, very frequently facing this typeerror. "TypeError: a bytes-like object is required, not 'str'". I want to define it as string only. I read to encode str objects in one of the posts. but it gives an error like "pass arguments to encode()", it is not working. Is there any permanent solution for this? like importing or defining something at the beginning only.
Thank You.
My code is as follows.
ids = [1,2,3,4,5]
list_ = ['A','B','X','Y','Z','W']
df = [None for i in ids]
print(type(df))
TypeError: a bytes-like object is required, not 'str'
If you are looking to convert a string object to byte object you should do something
like this
st = "Roushan" # a string object
byte_object = st.encode('utf-8')
here byte_object is the actual object and 'utf-8' is encoding scheme
There are a lot of encoding schemes
ASCII
UTF-16
For more on types of encoding Encoding
After this just figure out which argument was needed to be passed as byte instead of str and change that object to byte.
As i dont have nk module installed in my system i leave this to you
EDIT:
open a fresh new empty python file
write the following code
ids = [1,2,3,4,5]
list_ = ['A','B','X','Y','Z','W']
df = [None for i in ids]
print(type(df))
open a terminal and execute
python2.7 mycode.py
python3.7 mycode.py
in case 1 you get
in case 2
and please post the error Traceback you get . I believe the error is originating at other part of the code as this one is fine

how to return values from map function on dataframe

I am trying to return values from map function but instead it gives me the memory address. I tried using list, but then it gives me an error stating str object doesn't have an attribute decode. Is there a way out?
The first problem you mentioned is probably because you are accessing an objects name rather than applying a method.
But for this error Error: AttributeError: 'str' object has no attribute 'decode'
This error is already answered in here 'str' object has no attribute 'decode'. Python 3 error?
As it said here you are trying to decode an object that is already decoded. You have a str, there is no need to decode from UTF-8 anymore. If you remove the .decode() method from your chain it will be ok.

Categories