#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    41
    Rep Power
    2

    Sending output to CSV file


    Hello,

    I have created a script which I will use to populate a database but I am having problems extracting my output to a .csv file.

    This is my script:
    [code="Script"]
    import csv
    import random
    import decimal

    cardNumbers = []
    PIN = lambda: random.randint(0000,9999)
    Balance = lambda: random.uniform(0000,9999)

    cNStart = 4921000000000000
    cNMax = 4921000000000100

    while cNStart != cNMax:
    print cNStart, "%04d" % PIN(), "%.2f" % Balance()
    cardNumbers.append(cNStart)
    cNStart = cNStart + 1
    [/code]

    And this is my output:
    [code="Output"]
    4921000000000000 8045 5251.46
    4921000000000001 1312 2185.45
    4921000000000002 7450 162.82
    4921000000000003 9877 3636.08
    4921000000000004 7457 2459.36
    4921000000000005 2923 7599.29
    4921000000000006 1797 9755.81
    4921000000000007 9851 410.60
    4921000000000008 1704 2210.43
    4921000000000009 3172 6749.70
    4921000000000010 9731 2872.12
    4921000000000011 9799 430.08
    4921000000000012 5206 9654.64
    4921000000000013 2587 8650.92
    [/code]

    My output will go from 4921000000000000 all the way through to 4921999999999999 so how will I go about outputting all these returned outputs to a .CSV?

    Any help will be much appreciated.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    41
    Rep Power
    2
    Originally Posted by Mastah^
    Hello,

    I have created a script which I will use to populate a database but I am having problems extracting my output to a .csv file.

    This is my script:
    [code="Script"]
    import csv
    import random
    import decimal

    cardNumbers = []
    PIN = lambda: random.randint(0000,9999)
    Balance = lambda: random.uniform(0000,9999)

    cNStart = 4921000000000000
    cNMax = 4921000000000100

    while cNStart != cNMax:
    print cNStart, "%04d" % PIN(), "%.2f" % Balance()
    cardNumbers.append(cNStart)
    cNStart = cNStart + 1
    [/code]

    And this is my output:
    [code="Output"]
    4921000000000000 8045 5251.46
    4921000000000001 1312 2185.45
    4921000000000002 7450 162.82
    4921000000000003 9877 3636.08
    4921000000000004 7457 2459.36
    4921000000000005 2923 7599.29
    4921000000000006 1797 9755.81
    4921000000000007 9851 410.60
    4921000000000008 1704 2210.43
    4921000000000009 3172 6749.70
    4921000000000010 9731 2872.12
    4921000000000011 9799 430.08
    4921000000000012 5206 9654.64
    4921000000000013 2587 8650.92
    [/code]

    My output will go from 4921000000000000 all the way through to 4921999999999999 so how will I go about outputting all these returned outputs to a .CSV?

    Any help will be much appreciated.
    I've solved it.

    [Code="Amended coding"]
    import csv
    import random
    import decimal

    cardNumbers = []
    PIN = lambda: random.randint(0000,9999)
    Balance = lambda: random.uniform(0000,9999)

    cNStart = 4921000000000000
    cNMax = 4921000000000100
    c = csv.writer(open("test.csv","wb"))

    while cNStart != cNMax:
    #print cNStart, "%04d" % PIN(), "%.2f" % Balance()
    c.writerow([cNStart, "%04d" % PIN(), "%.2f" % Balance()])
    cardNumbers.append(cNStart)
    cNStart = cNStart + 1
    [/code]

IMN logo majestic logo threadwatch logo seochat tools logo