TypeError sequence item 0: expected str instance, bytes found - python

for some reason i keep getting the TypeError at this
TypeError Traceback (most recent call last)
<
ipython-input-19-3490eb36442d> in <module>
2 result, numbers = mail.uid('search', None, "ALL")
3 uids = numbers[0].split()
----> 4 result, messages = mail.uid('fetch', ','.join(uids), '(BODY[])')
mail.select("INBOX")
result, numbers = mail.uid('search', None, "ALL")
uids = numbers[0].split()
result, messages = mail.uid('fetch', ','.join(uids), '(BODY[])')
date_list = []
from_list = []
message_text = []
for _, message in messages[::2]:
msg = email.message_from_string(message)
if msg.is_multipart():
t = []
for p in msg.get_payload():
t.append(p.get_payload(decode=True))
message_text.append(t[0])
else:message_text.append(msg.get_payload(decode=True))
date_list.append(msg.get('date'))
from_list.append(msg.get('from'))
date_list = pd.to_datetime(date_list)
print (len(message_text))
print (len(from_list))
df = pd.DataFrame(data={'Date':date_list,'Sender':from_list,'Message':message_text})
print (df.head())
df.to_csv('~inbox_email.csv',index=False)

This line
result, messages = mail.uid('fetch', ','.join(uids), '(BODY[])')
is raising the exception
TypeError sequence item 0: expected str instance, bytes found
Inspecting the line, 'fetch' and '(BODY[])' are already strings, so they are unlikely to be the problem.
That leaves ','.join(uids). uids is actually a list of bytes instances, so str.join is raising the exception because it expects an iterable of str instances.
To fix the problem, decode numbers[0] to str before manipulating it.
result, numbers = mail.uid('search', None, "ALL")
uids = numbers[0].decode().split() # <- decode before splitting
result, messages = mail.uid('fetch', ','.join(uids), '(BODY[])')

Related

sequence item 0: expected str instance, tuple found(2)

I analyzed the data in the precedent and tried to use topic modeling. Here is a
syntax I am using:
According to the error, I think it means that the string should go in when
joining, but the tuple was found. I don't know how to fix this part.
class FacebookAccessException(Exception): pass
def get_profile(request, token=None):
...
response = json.loads(urllib_response)
if 'error' in response:
raise FacebookAccessException(response['error']['message'])
access_token = response['access_token'][-1]
return access_token
#Join the review
word_list = ",".join([",".join(i) for i in sexualhomicide['tokens']])
word_list = word_list.split(",")
This is Error
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
C:\Users\Public\Documents\ESTsoft\CreatorTemp\ipykernel_13792\3474859476.py in <module>
1 #Join the review
----> 2 word_list = ",".join([",".join(i) for i in sexualhomicide['tokens']])
3 word_list = word_list.split(",")
C:\Users\Public\Documents\ESTsoft\CreatorTemp\ipykernel_13792\3474859476.py in <listcomp>(.0)
1 #Join the review
----> 2 word_list = ",".join([",".join(i) for i in sexualhomicide['tokens']])
3 word_list = word_list.split(",")
TypeError: sequence item 0: expected str instance, tuple found
This is print of 'sexual homicide'
print(sexualhomicide['cleaned_text'])
print("="*30)
print(twitter.pos(sexualhomicide['cleaned_text'][0],Counter('word')))
I can't upload the results of this syntax. Error occurs because it is classified as spam during the upload process.

How to remove/skip <class 'NoneType'> object in Python

I am receiving the data from SOAP API call from a vendor and using Zeep library. The data is class 'NoneType' that is impossible to iterate through. My task is to remove/skip the NoneType object.
If I receive a response that contains some values, I am able to jsonify it, however, if the response returns None - I can't jsonify it or remove it.
For instance, I passed two lists of parameters and received two responses back, one that contains the data, the other one is None.
My code below:
# Making a SOAP call and save the response
response = client.service.GetOrders(**params[0])
# convert the response object to native Python data format
py_response = helpers.serialize_object(response)
# jsonify (list of dictionaries)
response_list = json.loads(json.dumps(py_response, indent=4, sort_keys=True, default=str))
print(type(response_list))
print(response_list)
So the output is the following:
<class 'list'> # successfully converted
[{'AgentID': 0, 'AgentName': 'Not specified', 'CustomerID': 1127}]
<class 'NoneType'> # was not converted
None
I have tried:
clean_response_list = [x for x in response_list if x != None]
Error: TypeError: 'NoneType' object is not iterable
clean_response_list = [x for x in response_list if x != None]
This doesn't work because response_list is None, so you can't iterate over it.
Try:
response_list = response_list or []
Or
if response_list is None:
response_list = []
Or
if py_response is not None:
response_list = json.loads(json.dumps(py_response, indent=4, sort_keys=True, default=str))
else:
response_list = []
Or
if py_response:
response_list = json.loads(json.dumps(py_response, indent=4, sort_keys=True, default=str))
else:
response_list = []

TypeError: can't concat int to bytes

Running this code in order to read email message:
if uid > uid_max:
result, data = server.uid('fetch', uid, '(RFC822)') # fetch entire message
msg = email.message_from_string(data[0][1])
I'm getting this error:
TypeError: can't concat int to bytes
How to fix this?
I had the same problem, I solved it by replacing uid with str(uid)
result, data = server.uid('fetch', str(uid), '(RFC822)')
bye bye

How to troubleshoot and resolve "TypeError: 'NoneType' object is not subscriptable" in python

I have a piece of code that fetches data from the ticketmaster API using a function I've named get_event_info. The first revision of the code worked as desired, subsequently I modified the original function to make use of header based authentication instead of URL based. I also added a few lines to the function which were intended to validate the response status code. After making these changes the code began producing the following TypeError:
Traceback (most recent call last):
File "ticketmaster_only_w_headers.py", line 146, in <module>
for event in ticket_search["_embedded"]["events"].items():
TypeError: 'NoneType' object is not subscriptable
I've read quite a bit about this type of error but I'm still unable to determine why my code is producing it in this instance. I would really appreciate an explanation on why my code is producing this error and what troubleshooting methods I should have used to uncover the source error. I'm fairly comfortable with programming but certainly no expert so the simpler the language used the better.
(Function Definition)
def get_event_info(search):
if search in CACHE_DICTION:
d = CACHE_DICTION[search]
else:
api_url = '{0}events/'.format(api_url_base)
payload = {"keyword": search, "apikey": api_token,
"format": "json", "dmaId": "366", "size": 200, "radius": "2"}
response = requests.get(api_url, headers=headers, params=payload)
if response.status_code == 200:
d = json.loads(response.text)
CACHE_DICTION[search] = d
f = open(CACHE_FNAME, 'w')
f.write(json.dumps(CACHE_DICTION))
f.close()
else:
d = None
return d
(Code snippet that triggers the error)
ticket_search = get_event_info("")
for event in ticket_search["_embedded"]["events"]:
a = event["id"]
b = event["name"]
if "dateTime" in event["dates"]["start"]:
c = event["dates"]["start"]["dateTime"].replace(
"T", " ").replace("Z", "")
else:
c = "NONE"
if "end" in event["dates"] and "dateTime" in event["dates"]["end"]:
j = event["dates"]["end"]["dateTime"].replace(
"T", " ").replace("Z", "")
else:
j = "NONE"
(Code that creates, opens, and writes to the cache used in the above code)
CACHE_FNAME = "ticketmaster_cache.json"
try:
cache_file = open(CACHE_FNAME, "r")
cache_contents = cache_file.read()
CACHE_DICTION = json.loads(cache_contents)
cache_file.close()
except:
CACHE_DICTION = {}
The previous revision of the get_event_info function shown below which does not produce any TypeError:
def get_event_info(search, ticketmaster_key = ticketmaster_key):
if search in CACHE_DICTION:
d = CACHE_DICTION[search]
else:
data = requests.get("https://app.ticketmaster.com/discovery/v2/events",
params = {"keyword": search, "apikey": ticketmaster_key,
"format":"json", "dmaId": "366", "size": 200, "radius": "2"})
print(data.url)
d = json.loads(data.text)
CACHE_DICTION[search] = d
f = open(CACHE_FNAME, 'w')
f.write(json.dumps(CACHE_DICTION))
f.close()
return d
Traceback & Error message I see when I run the latest revision of the code:
Traceback (most recent call last):
File "ticketmaster_only_w_headers.py", line 146, in <module>
for event in ticket_search["_embedded"]["events"]:
TypeError: 'NoneType' object is not subscriptable
Whenever you have a function that can explicitly return None, you should always check the return value first:
def func(a):
if a == 1:
return list(range(10)) # could return a list
else:
return None # or it could return None
a = 10
f = func(a)
f[1]
# raises TypeError: NoneType is not subscriptable
# check for NoneType first
if f is not None:
print(f[1])
# otherwise, kick out different result
else:
print('Got "None" for f!')
# Got "None" for f!
Your ticket_search is returned as None, but because your for loop is trying to do a key-lookup, it's failing, because None doesn't support that operation. Your logic, following from the above, should look like:
if ticket_search is not None:
for event in ticket_search["_embedded"]["events"]:
a = event["id"]
else:
raise TypeError
# or do something else
Well, the interpreter is explicitly telling you that you are trying to evaluate something like a[i], where a is None (instead of the intended type, like a list or a dict). In your case, it is either ticket_search itself, or ticket_search["_embedded"].
In any case, if you can rerun your code at all, putting a print(ticket_search) under ticket_search = get_event_info("") should make everything clear.

why when i pass a parameter in str this will be byte

i have a function that receive a str value but when i execute the error say that is a byte value:
Traceback (most recent call last):
File "C:\Users\sdand\Documents\Python\Engine\engine.py", line 4, in <module>
print (find.crawl_web('https://google.com',4))
File "C:\Users\sdand\Documents\Python\Engine\finder.py", line 68, in crawl_web
links = self.get_all_links(content)
File "C:\Users\sdand\Documents\Python\Engine\finder.py", line 20, in get_all_links
url, endpos = self.get_next_target(page)
File "C:\Users\sdand\Documents\Python\Engine\finder.py", line 7, in get_next_target
start_link = s.find('<a href=')
TypeError: a bytes-like object is required, not 'str'
this is the function where i call get_all_links:
def crawl_web(self,seed, max_depth):
tocrawl = [seed]
crawled = []
next_depth = []
depth = 0
index=[]
while tocrawl and depth <= max_depth:
page = tocrawl.pop()
if page not in crawled:
#here content content is str
content = self.get_page(page)
self.add_page_to_index(index,page,content)
links = self.get_all_links(content)
self.union(next_depth,links)
crawled.append(page)
if not tocrawl:
tocrawl, next_depth = next_depth, []
depth = depth+1
return index
this is get_page:
def get_page(self,url):
try:
import urllib.request
return urllib.request.urlopen(url).read()
except:
return ""
this is get_all_links:
def get_all_links(self,page):
#but here it is byte i dont now why
links=[]
while True:
url, endpos = self.get_next_target(page)
print(url)
if url != None:
links.append(url)
page = page[endpos:]
else:
break
return links
i don't now why my str variable "Content" is converted in byte type in get_all_links, somebody can explain this to me, and how i can resolve it?
As you may not be aware, .read() returns a byte object, not str, although using byte object is more recommended when web scraping, the easiest fix though is to cast it to str by decoding it.
return urllib.request.urlopen(url).read().decode('utf-8')

Categories

Resources