0

I have sequence of numbers:

6577

I would like to see them in the .csv file like this:

6  
5  
7

I've tried with file writer but it writes all numbers in first row.

sequence is string..

s = ['6','5','7','7','6']
item_length = len(s)

with open('test.csv', 'wb') as test_file:
    file_writer = csv.writer(test_file)
    for i in range(item_length):
        file_writer.writerow([x[i] for x in s])
5
  • A sequence? Is that a list? A string? An int? Commented Dec 20, 2012 at 11:36
  • 2
    Please post you code along with question, you will get much more useful feedback that way. Right now python -c "for d in '6577':print d" > file solves you problem. Commented Dec 20, 2012 at 11:36
  • check out xlwt module in python Commented Dec 20, 2012 at 11:38
  • s = '657' is not the same as 6577. Which is it? And if there's only one column, why do you need csv? Commented Dec 20, 2012 at 11:41
  • Your initial sequence and the expected .csv output and the sequence in the code example are still all different. Is the code meant to remove duplicates? Commented Dec 20, 2012 at 11:46

1 Answer 1

2

If you wish to write each list item as a row, you can try:

s = ['6','5','7','7','6']
item_length = len(s)

with open('test.csv', 'wb') as test_file:
    file_writer = csv.writer(test_file)
    for item in s:
        file_writer.writerow(item)

Also, with only a single column to write out, this might suffice:

s = ['6','5','7','7','6']
with open('test.csv', 'wb') as test_file:
   test_file.write("\n".join(s) + "\n")
Sign up to request clarification or add additional context in comments.

2 Comments

I tried that...But i guess output cannot be writen in columns in .csv
Glad you found a solution.

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.