Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x ';TypeError:不支持解码str';在for循环中的for循环中追加str时_Python 3.x_List_Data Science - Fatal编程技术网

Python 3.x ';TypeError:不支持解码str';在for循环中的for循环中追加str时

Python 3.x ';TypeError:不支持解码str';在for循环中的for循环中追加str时,python-3.x,list,data-science,Python 3.x,List,Data Science,我正在做一个求职网站搜索工具,但是当我把一个打印语句转换成一个返回语句时,我遇到了一个“TypeError:不支持对str进行解码”,即使这个转换公式在非for-in-a-for-loop上工作 我试着删除一个str变量,结果成功了,但我需要的是一个包含公司和工作的双重列表 def get_company_and_jobs(): """this function scrapes the company names and job titles""" comps_and_

我正在做一个求职网站搜索工具,但是当我把一个打印语句转换成一个返回语句时,我遇到了一个“TypeError:不支持对str进行解码”,即使这个转换公式在非for-in-a-for-loop上工作

我试着删除一个str变量,结果成功了,但我需要的是一个包含公司和工作的双重列表

def get_company_and_jobs():
    """this function scrapes the company names 
    and job titles"""
    comps_and_jobs = []
    companyName = pageSoup.find_all('span', class_='company')
    jobTitle = pageSoup.find_all('div', class_='title')
    for span in jobTitle:
        for x in companyName:
            comps_and_jobs.append(str(x.text,span.text))
            # # This is before I added a list
            # print(x.text,span.text)
    return comps_and_jobs
有没有更好的方法可以在列表或字典中迭代结果以匹配工作和公司


我应该使用zip而不是列表吗?

因为我传递了两个参数,所以我只需将参数分成两行:

def get_company_and_jobs():
    """this function scrapes the company names 
    and job titles"""
    comps_and_jobs = []
    companyName = pageSoup.find_all('span', class_='company')
    jobTitle = pageSoup.find_all('div', class_='title')
    for span in jobTitle:
        for x in companyName:
            comps_and_jobs.append(str(x.text))
            comps_and_jobs.append(str(span.text))
    return comps_and_jobs

因为我传递了两个参数,所以我简单地将参数分成两行:

def get_company_and_jobs():
    """this function scrapes the company names 
    and job titles"""
    comps_and_jobs = []
    companyName = pageSoup.find_all('span', class_='company')
    jobTitle = pageSoup.find_all('div', class_='title')
    for span in jobTitle:
        for x in companyName:
            comps_and_jobs.append(str(x.text))
            comps_and_jobs.append(str(span.text))
    return comps_and_jobs

您正在将两个参数传递给
str
。第二个参数(如果存在)是编码说明符,例如
'utf-8'
或类似的东西。看起来是另外一回事。谢谢@TomKarzes,我添加了我使用的公式,并在那里提出了问题。您将两个参数传递给
str
。第二个参数(如果存在)是编码说明符,例如
'utf-8'
或类似的东西。看起来是另外一回事。谢谢@TomKarzes,我添加了我使用的公式,并在那里提出了问题。
def get_company_and_jobs():
    """this function scrapes the company names 
    and job titles"""
    comps_and_jobs = []
    companyName = pageSoup.find_all('span', class_='company')
    jobTitle = pageSoup.find_all('div', class_='title')
    for span in jobTitle:
        for x in companyName:
            comps_and_jobs.append(str(x.text))
            comps_and_jobs.append(str(span.text))
    return comps_and_jobs