Writing a list of strings into a csv

Viewed 475

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)

output csv

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:

strings with spaces

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!!

2 Answers

The reason is because writer.writerow is expecting you to pass something which can be iterated - a list, a tuple or anything else. When you pass it a string it is considered an iterable - a sequence of characters. So to fix this you need to put square brackets around item at the call: make it writer.writerow([item]) instead of writer.writerow(item).

Replace your current code with the following.

I am using pandas.

import pandas as pd
import csv

h3List= []

soup = BeautifulSoup(open('test.html'),'html.parser')

h3 = soup.find_all('h3')

for text in h3:
    h3List.append(text.text)
df = pd.DataFrame(h3List)
df.to_csv('h3_test.csv')


Related