0

The following code does not output any data except: col1 [none] in my .csv file. There are no errors though.

I am hoping to get one column with about 50 rows of text rows. Thanks for any help in advance. If it is possible to put the name, workab and abstract in separate columns that would also be of great help.

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()
grob
  • 111
  • 1
  • 8
  • You can use `pandas` to save to csv, It is very easy. Refer this [guide](https://chrisalbon.com/python/data_wrangling/pandas_saving_dataframe_as_csv/) – CodeIt Oct 03 '18 at 15:25
  • 1
    @CodeIt: Did you even read his code? His method of writing the csv is not his issue. His functions `output()` and `output2()` return no values. `output()` doesn't even collect a result it could collect. `output2()` iterates once which means the iteration is pointless. – Steven Rumbalski Oct 03 '18 at 15:28

1 Answers1

0

It seems like you're calling output() inside output2(), but your output() function has no return values.

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

In addition, your output2() function doesn't return anything either, which is why myData = ['Col1',[output2()]] is giving you col1 [none].

Q. Tang
  • 1
  • 1