0

So I have a solved problem, but I don't like the solution :)

with open(outfile, 'a') as file:
    writer = csv.writer(file) #opens a csv writer
    #inserts ID to front of wordList
    wordList.insert(0,ID)
    writer.writerow(wordList)
    #removes ID
    wordList.remove(ID)

Right now it successfully writes an ID and a list of words into csv form (as far as I can tell-- I don't actually have excel on my computer). The part I don't like is that I have to insert and remove the ID, because I don't want it included later. This seems lame. Can I somehow do a double insert to the same row, easily?

I tried:

 writer.writerow([ID,wordList])

but it gave undesirable square brackets

This is for 2.7, if that matters! Thanks !

3 Answers 3

2
writer.writerow([ID] + wordList)

or

from itertools import chain

writer.writerow(chain([ID], wordList))
Sign up to request clarification or add additional context in comments.

2 Comments

I know in this situation these would function the same, but is there an advantage (performance or otherwise) that would make one better than the other for a larger scale? Thanks!
itertools.chain will concatenate an arbitrary number of sequences doc.
1

You could do writer.writerow([ID] + wordList).

1 Comment

Python never lets me down, neither does Stack Overflow!
1

You can try as follow:

writer.writerow([ID] + wordlist)

1 Comment

Great! I knew there was some python semantic wizardry that would do this

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.