我有此python代码输出,我想导出为python中的.csv文件?

时间:2018-10-03 15:21:23

标签: python export-to-csv

以下代码不输出任何数据,除了:.csv文件中的col1 [none]。不过没有错误。

我希望得到一列包含大约50行文本行。感谢您的任何帮助。如果可以将名称,workab和abstract放在单独的列中,这也将很有帮助。

def output():
    headernum = 0
    i = 0
    x = soup.find_all("h1")
    for i in range(len(x)):
        header = soup.find_all('h1')[headernum]
        name = header.find_all_next('p')[1]
        nameab = name.text
        #print(nameab.replace("\n",""))
        workplace = name.find_all_next('i')[0]
        workplace2 = name.find_all_next('i')[1]
        workab = workplace.text + workplace2.text
        #print(workab.replace("\n",""))
        abstract = []
        for elem in name.next_siblings:
            if elem.name == 'h1':
                break
            if elem.name != 'p':
                continue
            abstract.append(elem.get_text())
        a = "**".join(abstract).replace("\n", " ").encode('utf-8')
        i += 1
        headernum += 1

def output2():
    x = soup.find_all("h1")
    l = []
    for i in range(1):
        l.append(output())

def fileOut():
    myData = ['Col1',[output2()]]
    myFile = open('test.csv', 'w')
    with myFile:
       writer = csv.writer(myFile)
       writer.writerows([myData])
       myFile.close()

fileOut()

1 个答案:

答案 0 :(得分:0)

似乎您在output2()内调用output(),但是output()函数没有返回值。

def output2():
    x = soup.find_all("h1")
    l = []
    for i in range(1):
        l.append(output())

此外,您的output2()函数也不返回任何内容,这就是myData = ['Col1',[output2()]]给您col1 [none]的原因。