0

I have a list of names that I have scraped off a website and I want to put them into a CSV file with one name in the first column of each row.

I'm using python's CSV module as demonstrated in the Python documentation:

with open('/home/kwal0203/Desktop/eggs.csv', 'a') as csvfile:
...     writer = csv.writer(csvfile)
...     writer.writerow("Hello my friend")

The script saves each letter of the sentence in a new column. The only way I've found to get the sentence to save into one cell is using this script:

with open('/home/kwal0203/Desktop/eggs.csv', 'a') as csvfile:
...     writer = csv.writer(csvfile, delimiter = ' ')
...     writer.writerow("Hello my friend")

But now even though the output is in one cell it looks like this:

Hello""my""friend

Is there anyway to use the first method and get all the letters in one cell? or use the second method and get rid of the quotation marks?

Thanks any help appreciated.

1 Answer 1

3

You need to pass a row (a sequence of columns), not a single column:

with open('/home/kwal0203/Desktop/eggs.csv', 'a') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(["Hello my friend"])

Simply passing a string is considered as a sequence of characters. (String is a kind of sequence).

Sign up to request clarification or add additional context in comments.

Comments

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.