startswith first arg must be bytes or a tuple of bytes, not str: 'Python for everybody' Coursera

喜夏-厌秋 提交于 2020-05-17 07:11:00

问题


I am completing the 'Python for everybody' course on coursera. I am stuck on the 'Mailing List Data - Part I'

I have the following code below:

import sys
import sqlite3
import time
import ssl
from urllib import request 
from urllib.parse import urljoin
from urllib.parse import urlparse
import re
from datetime import datetime, timedelta

# Not all systems have this so conditionally define parser
try:
    import dateutil.parser as parser
except:
    pass


def parsemaildate(md):
    # See if we have dateutil
    try:
        pdate = parser.parse(tdate)
        test_at = pdate.isoformat()
        return test_at
    except:
        pass

    # Non-dateutil version - we try our best

    pieces = md.split()
    notz = " ".join(pieces[:4]).strip()

    # Try a bunch of format variations - strptime() is *lame*
    dnotz = None
    for form in ['%d %b %Y %H:%M:%S', '%d %b %Y %H:%M:%S',
                 '%d %b %Y %H:%M', '%d %b %Y %H:%M', '%d %b %y %H:%M:%S',
                 '%d %b %y %H:%M:%S', '%d %b %y %H:%M', '%d %b %y %H:%M']:
        try:
            dnotz = datetime.strptime(notz, form)
            break
        except:
            continue

    if dnotz is None:
        # print 'Bad Date:',md
        return None

    iso = dnotz.isoformat()

    tz = "+0000"

    try:
        tz = pieces[4]
        ival = int(tz)  # Only want numeric timezone values
        if tz == '-0000': tz = '+0000'
        tzh = tz[:3]
        tzm = tz[3:]
        tz = tzh + ":" + tzm
    except:
        pass

    return iso + tz


conn = sqlite3.connect('emreyavuzher.sqlite')
cur = conn.cursor()
conn.text_factory = str

baseurl = "http://mbox.dr-chuck.net/sakai.devel/"

cur.execute('''CREATE TABLE IF NOT EXISTS Messages 
    (id INTEGER UNIQUE, email TEXT, sent_at TEXT, 
    subject TEXT, headers TEXT, body TEXT)''')

start = 0
cur.execute('SELECT max(id) FROM Messages')
try:
    row = cur.fetchone()
    if row[0] is not None:
        start = row[0]
except:
    start = 0
    row = None

print(start) 

many = 0

# Skip up to five messages
skip = 5
while True:
    if (many < 1):
        sval = input('How many messages:')
        if (len(sval) < 1): break
        many = int(sval)

    start = start + 1
    cur.execute('SELECT id FROM Messages WHERE id=?', (start,))
    try:
        row = cur.fetchone()
        if row is not None: continue
    except:
        row = None

    many = many - 1
    url = baseurl + str(start) + '/' + str(start + 1)

    try:
        ctx = ssl.create_default_context()
        ctx.check_hostname = False
        ctx.verify_mode = ssl.CERT_NONE
        document = request.urlopen(url)

        text = document.read()
        if document.getcode() != 200:
            print("Error code=", document.getcode(), url) 
            break
    except KeyboardInterrupt:
        print('')
        print('Program interrupted by user...')
        break
    except:
        print("Unable to retrieve or parse page", url)
        print(sys.exc_info()[0]) 
        break

    print(url, len(text)) 

    if not text.startswith('From '):
        if skip < 1:
            print(text) 
            print("End of mail stream reached...") 
            quit()
        print("Skipping badly formed message") 
        skip = skip - 1
        continue

However, the code keeps giving me the error: Traceback (most recent call last): File "", line 128, in TypeError: startswith first arg must be bytes or a tuple of bytes, not str

Would anybody be able to give me a helping hand?

来源:https://stackoverflow.com/questions/61565097/startswith-first-arg-must-be-bytes-or-a-tuple-of-bytes-not-str-python-for-eve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!