0

i have a problem with writing into a csv file. First the "lists" in the code would return a list that has the usernames with a lot of gaps and does not show properly. Secondly once I try to write it in the csv file I would get each character of the username in a different cell in excel.

enter image description here

for i in range(0,33):
    link = (df.link.iloc[i])
    source1 = urllib.request.urlopen(link).read()
    soup1 = bs.BeautifulSoup(source1,'lxml')
    for username in soup1.find_all('div', class_="user-name"):
        lists.append(username.text)
#    for time in soup1.find_all('div',class_="thread-ago"):
example = open('generalinfo.csv','w')
wr = csv.writer(example,quoting = csv.QUOTE_ALL)        
wr.writerows(lists)    
example.close()  
0

2 Answers 2

1

writerows() needs a list of lists, try::

for username in soup1.find_all('div', class_="user-name"):
    lists.append([username.text])
Sign up to request clarification or add additional context in comments.

Comments

0

wr.writerows(lists) expects a list of lists of strings. In your snippet, it is a list of strings. Each string (being a sequence) is implicitly converted to a list of characters, and each character is written in its own cell. Here's how to fix:

lists.append([username.text])

1 Comment

Thank you! Do you happen to know how can I avoid having all this gaps in the lists on python?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.