0

Example strings:

uji708
uhodih
utus29
agamu4
azi340
ekon62

I need to change them into CSV list like this:

uji708,uhodih,utus29,  
agamu4,azi340,ekon62,

My code so far:

email = 'mail_list.txt'
handle = open(email)

for line in handle:
    try:
        email = line.split()[0].replace('\n', '')
        l = line.split()
        print '\n'.join((','.join(x) for x in zip(l[::3], l[1::3], l[2::3])))    
    except:
        print 'error'

How can I do this in Python?

3
  • thanks reply, actually im handling email list. can i use such like follow style? maillist = 'scrap_daum.txt' try: handle = open(maillist) except: exit(1) for line in handle: #currline += 1 valid = [] try: email = line.split(':')[0].replace('\n', '') except: exit(1) Commented Nov 4, 2009 at 7:19
  • hello i was update pastebin site i want to handle email list Commented Nov 4, 2009 at 7:33
  • elca.pastebin.com/d4556c585 here is updated site Commented Nov 4, 2009 at 7:34

2 Answers 2

2

Use csv.writer:

import csv
import sys

writer = csv.csvwriter(sys.stdout)
writer.writerow(iterable_containing_my_strings)
Sign up to request clarification or add additional context in comments.

1 Comment

iterable_containing_my_strings = open('mail_list.txt')
0

Here is a very specific answer to a very specific question when you will clarify/generalize your question I may update my answer

s = """
uji708
uhodih
utus29
agamu4
azi340
ekon62
"""
l = s.split()
print '\n'.join((','.join(x) for x in zip(l[::3], l[1::3], l[2::3])))

1 Comment

hello thanks for your reply.. can i use some other method? i was updated pastebin :) elca.pastebin.com/d4556c585

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.