I am a beginner Python programmer and I have been unsuccessful in looking for a solution to my issue in this forum. It may be because I am unable to describe my problem in the right jargon - I apologize in advance if this is the case.
I used BeautifulSoup to extract a list of strings from an html document. I want to transfer this list of strings into a csv (1 string per row in a single column). However, my program is placing each character of each string in its own column
import csv
h3List= []
soup = BeautifulSoup(open('test.html'),'html.parser')
h3 = soup.find_all('h3')
for text in h3:
h3List.append(text.text)
with open('h3List.csv','w') as f:
writer = csv.writer(f)
for item in h3List:
print(item)
writer.writerow(item)
As you can see in the image above, the output csv file places each character in each string in individual columns. I want the 'test' strings in 1 column only.
I tried this solution:
for item in h3List:
print(item)
writer.writerow([item])
But when my string contains spaces, for example, the string 'test hello' - the output csv separates the words in individual columns as seen below:
What is wrong with my code? For the life of me, I can't seem to figure it out.
If the solution already exists in this forum, I would appreciate a link to the post. Thank you!!
