1

I have written a python web scraper and would like to output the data strings I have gotten into a csv/excel file. So far, I have a for loop that accesses multiple database websites and stores the data in a string. I would like to pop out these strings each time I complete the web scraping before moving onto the next page.

Someone suggested to create a whole repository of them or a dictionary and then reference it. I tried implementing it, but my code instead returns me a the data in one cell instead of spanning multiple cells because I have a header at the top that separates the data into my desired attributes.

Substances = []
Whole_list = []
f = open(filename)  # chemtest.txt 
for sub in f: 
    Substances.append(sub)
    print sub

for substance in Substances: 
    #some logic 
    names1 = [data ]
    Whole_list.append(names1)

with open('chemtest.csv', 'wb') as myfile:  #creates new chemtest.csv
    wr = csv.writer(myfile)
    wr.writerow(Whole_list)

So far I'm running through 2 websites as a test and my outputs are:

names1 = ['Acetaldehyde', 'Acetaldehyde', '75-07-0', 'GO1N1ZPR3B', 'CC=O']
Whole_list = [['Acetaldehyde', 'Acetaldehyde', '75-07-0', 'GO1N1ZPR3B', 'CC=O']]
names1 = ['Acetone', 'Acetone', '67-64-1', '1364PS73AF', '=O']
Whole_list = [['Acetaldehyde', 'Acetaldehyde', '75-07-0', 'GO1N1ZPR3B', 'CC=O'], ['Acetone', 'Acetone', '67-64-1', '1364PS73AF', '=O']]
 

What is wrong with my method exactly and how can I improve it?

1 Answer 1

2

Use writerows (note the s at the end). writerow is for writing one line at a time.

wr.writerows(Whole_list)

As a side note, capitalized variable names are usually reserved to classes, so prefer whole_list.

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

2 Comments

This, and make sure you're using the appropriate delimiter - if all your values appear in one cell, then your Excel version might be expecting something other than a comma between columns - European Excels expect semicolons, for example. Try setting delimiter=";" when creating the writer object.
Okay, thanks! @TimPietzcker: That's interesting. I'll be sure to keep that in mind. Thanks!

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.