Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Convert to CSV?

  1. #1
    New to the CF scene
    Join Date
    Oct 2016
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Convert to CSV?

    st = """ 2 Joe 95000 4 Steve
    35000 1 Samantha 150000 10 Leah 99000
    6 Riley 53215 7 Ashley 23424
    15 Sheyanne 225000 9 Dave 35235 """
    Ids = ['2', '4', '1', '10', '6', '7', '15', '9']
    Names = ['Joe', 'Steve', 'Samantha', 'Leah', 'Riley', 'Ashley', 'Sheyanne', 'Dave']
    Salaries = ['95000', '35000', '150000', '99000', '53215', '23424', '225000', '35235']
    print(Ids)
    print(Names)
    print(Salaries)

    how do i convert this python code to output to csv?

  2. #2
    New Coder
    Join Date
    Nov 2016
    Location
    South Africa, Cape Town
    Posts
    49
    Thanks
    1
    Thanked 1 Time in 1 Post
    Which part would you like to be displayed as CSV's ? If you want the first string to be printed as CSV's you can use:

    Code:
    st = "2 Joe 95000, 4 Steve 35000, 1 Samantha 150000, 10 Leah 99000, 6 Riley 53215, 7 Ashley 23424, 15 Sheyanne 225000, 9 Dave 35235 "
    
    Ids = ['2', '4', '1', '10', '6', '7', '15', '9']
    Names = ['Joe', 'Steve', 'Samantha', 'Leah', 'Riley', 'Ashley', 'Sheyanne', 'Dave']
    Salaries = ['95000', '35000', '150000', '99000', '53215', '23424', '225000', '35235']
    
    for x in st.split(","):
        print x
    or

    Code:
    st = "2 Joe 95000, 4 Steve 35000, 1 Samantha 150000, 10 Leah 99000, 6 Riley 53215, 7 Ashley 23424, 15 Sheyanne 225000, 9 Dave 35235 "
    
    Ids = ['2', '4', '1', '10', '6', '7', '15', '9']
    Names = ['Joe', 'Steve', 'Samantha', 'Leah', 'Riley', 'Ashley', 'Sheyanne', 'Dave']
    Salaries = ['95000', '35000', '150000', '99000', '53215', '23424', '225000', '35235']
    
    CSVList = []
    
    for i, n, s in zip(Ids, Names, Salaries):
        CSVList.append("{} {} {}".format(i,n,s))
                
    print CSVList
    I'm sure you can improvise on this.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •