0

I have a for loop that prints 4 details:

deats = soup.find_all('p')
    for n in deats:
        print n.text

The output is 4 printed lines.

Instead of printing, what I'd like to do is have each 'n' written to a different column in a .csv. Obviously, when I use a regular .write() it puts it in the same column. In other words, how would I make it write each iteration of the loop to the next column?

1
  • use writerow() from csv as I have suggested! Commented Feb 27, 2014 at 20:16

4 Answers 4

2

You would create the csv row as a loop (or using list comprehension) I will show the explicit loop for ease of reading and you can change it to a single list comprehension line yourself.

row = []
for n in deats:
  row.append(n)

Now you have row ready to write to the .csv file using csv.Writer()

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

Comments

1

Hei, try like this:

import csv

csv_output = csv.writer(open("output.csv", "wb"))   # output.csv is the output file name!
csv_output.writerow(["Col1","Col2","Col3","Col4"]) # Setting first row with all column titles

temp = []
deats = soup.find_all('p')
for n in deats:
    temp.append(str(n.text))
csv_output.writerow(temp)

6 Comments

Hi S.M. so, I'm a little confused by this. Does this add a new row before writing?
yes, it does! If you don't need that, then remove this line: csv_output.writerow(["Col1","Col2","Col3","Col4"]) # Setting first row with all column titles
sure, but suppose that the deats loop is nested inside of another for loop. What happens when I get to the deats loop again? Is it possible to set it up to add a new to start writing the next iteration?
to add a new row? you mean?
Yes. so, imagine: ln 1: for x in range(1,4) ln 2:name = "Joe" ln3 for n in deats ln4 print n.text I'd want to not only put the deats into the row, but also name. and this is going to happen 3 times because of the for loop at the top. So, for each iteration of the loop, I'll need a new row added, and I'll need to write to that row.
|
1

You use the csv module for this:

import csv
with open('output.csv', 'wb') as csvfile:
    opwriter = csv.writer(csvfile, delimiter=','
    opwriter.writerow([n.text for n in deats])

Comments

1
extra_stuff = pie,cake,eat,too
some_file.write(",".join(n.text for n in deats)+"," + ",".join(str(s) for s in extra_stuff))

??? is that all you are looking for?

7 Comments

Yes, this works. I'm embarrassed. Maybe this is a little harder: suppose that, in addition to the n deats, I also want to write 5 other things to the same row? Can I do that in a single line of python? The other variables are, say, "pie", "cake", "eat", "too".
I think it's worth noting that this will not handle the cases where n.text contains commas or newlines. That is why everyone else is recommending the csv module.
sure worth noting ... 9/10 times i find the csv module to be overkill for this sort of thing (although for html content maybe not)
Hmm, I get a type error because the function only takes 1 argument but 3 are given. I should clarify, pie = "pie" and I'd like to write pie to the next column. I think that ...for n in deats) + pie) returns the error. Thank you for your help.
@JoranBeasley: Don't forget the comma between the deats stuff and the extra_stuff. It's starting to get to the point where it might be more readable to build a single combined list before doing any joins.
|

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.