1

I have a problem. If I explain it in words, I will mess up, so here it is in pictures.

my code:

import csv

def mytest():
    s1 = 'post'
    s2 = 'a_num'
    s3 = ['personA','GenderA','personB','GenderB','personC','GenderC','personD','GenderD','personE','GenderE',]
    s4 = ['comment by F','comm_F_gender','comment','ano_num','comment by G','comm_G_gender','comment','ano_num','comment by H','comm_H_gender','comment','ano_num']      
    with open('mytestfb.csv', 'a') as csvfile:
        spamwriter = csv.writer(csvfile, delimiter=',',
                                quotechar='|', quoting=csv.QUOTE_MINIMAL)
        spamwriter.writerow([s1.encode('utf-8'),s2.encode('utf-8')])
        x = 0
        while(x < len(s3)):
            spamwriter.writerow(['','',s3[x].encode('utf-8'),s3[x+1].encode('utf-8')])
            x += 2
        x = 0
        while(x < len(s4)):
            spamwriter.writerow(['','','','',s4[x].encode('utf-8'),s4[x+1].encode('utf-8'),s4[x+2].encode('utf-8'),s4[x+3].encode('utf-8')])
            x += 4

This is what this code is doing:

enter image description here

this is what I need the code to do:

enter image description here

2
  • 4
    Your code, as posted, does neither. Commented Jul 2, 2013 at 19:49
  • @MartijnPieters whoops! wrong code lol. it has now been edited to make it match Commented Jul 2, 2013 at 19:59

1 Answer 1

1

That can't be the whole code can it? So if I understand every four items in s4 and two items in s3 represent a row.

def mytest():
    s1 = 'thing1'
    s2 = 'a_number'
    s3 = ['personA','GenderA','personB','GenderB','personC','GenderC','personD','GenderD','personE','GenderE',]
    s4 = ['comment by F','comm_F_gender','comment','ano_num','comment by G','comm_G_gender','comment','ano_num','comment by H','comm_H_gender','comment','ano_num']      
    with open('mytestfb.csv', 'a') as csvfile:
        spamwriter = csv.writer(csvfile, delimiter=',',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
        exportlist=[s1,s2]
        for count in range(len(s3)/2):
            exportlist+=s3[count*2-1:count*2]
            try:
                exportlist+=s4[count*4-1+count*4]
            except IndexError:
                pass
        spamwriter.writerow(exportlist.encode('utf-8'))
Sign up to request clarification or add additional context in comments.

3 Comments

No, it isnt the whole code but it is the part I am having trouble with. In a nutshell, I am extracting information from online and putting it into excel. I extract it into this format.
Sorry: that was responding to your original post. Basically, when you use 'writerow' it creates a new row, which I don't believe you can add to without closing the file and reading it again. Or doing what I suggested. Hope it was helpful
Sorry also realized that you will get an index error with that, and so I fixed it to duplicate what I think you are going for. Sorry if I am offbase

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.