Hi there. I am currently writing a registration program in Python, and have come across a problem. In my hours of searching, there seems to be no way to split a list that has elements obtained from raw_input. Basically, I would like to take input from a user, feed it into an array, and then split that array with commas so that it can be imported into Excel.

Here is what I have so far:

def main():
    info = []
    name1 = raw_input("What is your name? ")
    info.append(name1)
    org_name = raw_input("What organization do you work for? ")
    info.append(org_name)
    title = raw_input("What is your title at this organization? ")
    info.append(title)
    addr1 = raw_input("What is the address of your organization, all on one line? ")
    info.append(addr1)
    email1 = raw_input("What is your email address? ")
    info.append(email1)
    phone1 = raw_input("What is your phone number? ")
    info.append(phone1)
    print "We will be writing your information to a file."
    print info

main()

I have already tried info.split(',') and it simply gives me errors. Thanks in advance.

Recommended Answers

All 3 Replies

The function split() is for strings, not for lists. In your program build up a CSV string rather than a list. Something like this:

def main():
    info = ""
    name1 = raw_input("What is your name? ")
    info += name1 + ','
    org_name = raw_input("What organization do you work for? ")
    info += org_name + ','
    title = raw_input("What is your title at this organization? ")
    info += title + ','
    addr1 = raw_input("What is the address of your organization, all on one line? ")
    info += addr1 + ','
    email1 = raw_input("What is your email address? ")
    info += email1 + ','
    phone1 = raw_input("What is your phone number? ")
    info += phone1 + '\n'
    print "We will be writing your information to a file."
    print info

main()

"""my display screen shows for example -->

What is your name? Frank Marsh
What organization do you work for? ACME Co
What is your title at this organization? Helper
What is the address of your organization, all on one line? 123 West St Morghantown WV
What is your email address? framar@hotmail.net
What is your phone number? 123-456-7890
We will be writing your information to a file.
Frank Marsh,ACME Co,Helper,123 West St Morghantown WV,framar@hotmail.net,123-456-7890

"""

Sneekula code will work fine for CSV string.

Just make and other version,that do just the same.
Maybe better looking,not that it matter so much at all.

def question():
    my_question =\
    ['What is your name? ', 
     'What organization do you work for? ',
     'What is your title at this organization? ',
     'What is the address of your organization, all on one line? ',
     'What is your email address? ',    
     'What is your phone number? ',
     'We will be writing your information to a file. ']
    return my_question
    
def main():    
    my_list = []
    for i in range(7):
        s = raw_input(question()[i])
        my_list.append(s)
        make_string = ','.join(my_list)
    print make_string
    
if __name__ == "__main__":
    main()

Sneekula code will work fine for CSV string.

Just make and other version,that do just the same.
Maybe better looking,not that it matter so much at all.

It is recommended to let automatic line continuation to take care of continued line. I have my own style here is how I would write the same code.

""" This module takes question from array questions_<language>
    and adds corresponding input from user to list my_list.
    It prepares separator joined list for variable tofile for saving
"""
## semicolon or tab is better choice for separator as it does not occur inside addresses
separator = ';'

questions_uk = [
 'What is your name? ', 
 'What organization do you work for? ',
 'What is your title at this organization? ',
 'What is the address of your organization, all on one line? ',
 'What is your email address? ',    
 'What is your phone number? ',
 'We will be writing your information to a file. ']
    
if __name__ == "__main__":
    my_list = []
    for i in questions_uk:
        s = raw_input(i)
        my_list.append(s)

    tofile = separator.join(my_list)
    ##########
    ## demonstration debug
    print tofile
    ## also is possible to do this if necessary
    q=4
    print 'Question',q
    print questions_uk[q],my_list[q]
    ##########
#### put file writing here when desided about filenames etc.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.