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

    Join Date
    Oct 2004
    Posts
    3
    Rep Power
    0

    Arrow Noob script help.


    I am very new to Python, but I think it is the best language to use for my current issue.

    Here is what I am trying to do:

    I have a file with several columns of tab deliminated text, rach row being a different record.

    I need to re-arrange that file, on a regular basis (which is why I am thinking python script) to a CSV file, with the format: [column2]@[column1],MailServerName,[column2]@[column1],[column5],YES,YES,NO

    where mailservername is a constant, and 'yes,yes,no' is just text.

    I am having a heck of a time getting anything to work. specifically, successfully importing the tab deliminated file for use.

    Can anyone give me some pointers on this? Or a well commented example script?

    It is a win2k box, if that makes any difference.

    Thank you in advance,

    Kyle
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    If the text in the tab delimited file is not quoted in any way then you can read in the text a line at a time and use split('\t') to separate out the fields.

    If the format is more complex, ie fields may contain tabs if they are in double quotes, then you can use the csv module to parse the lines. You can also use the csv module to create properly formatted output. The csv module has predefined formats for Excel comma delimited and tab delimited files, and you can define your own formats.


    this should do something like what you want (untested code ahead - caveat emptor)...

    Code:
    import csv
    input = open('my/input/file.txt')
    output = csv.writer(open('the/output/file.csv', 'w'))
    
    for line in input:
       fields = line.split('\t')
       addr = "%s@%s" % fields[1], fields[0]
       output.writerow( [addr, 'MailServerName', addr, fields[4], "YES", "YES", "NO"] )
    Notes:
    * lists are indexed from zero, so the column numbers are one less than in your post.
    * csv.writer defaults to Excel comma delimited format.

    Dave - The Developers' Coach
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    For completeness here is the version that uses the csv module to read the input too - this will correctly handle fields that are in quotes:

    Code:
    import csv
    input = csv.reader(open('my/input/file.txt'), 'excel-tab')
    output = csv.writer(open('the/output/file.csv', 'w'))
    
    for fields in input:
       addr = "%s@%s" % fields[1], fields[0]
       output.writerow( [addr, 'MailServerName', addr, fields[4], "YES", "YES", "NO"] )
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    3
    Rep Power
    0
    Thank you very much, I am sure this will prove to be invaluable.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    3
    Rep Power
    0
    New question, is there any way to stop or start a windows service through Python?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Originally Posted by Zaph0d042
    New question, is there any way to stop or start a windows service through Python?
    This should really be a new thread, but here goes...

    If you install the pywin32 extensions (http://sourceforge.net/projects/pywin32/ ), then you have access to most of the Windows operating system calls, including the windows services functions. so yes, you can.

    Dave - The Developers' Coach
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Posts
    17
    Rep Power
    0
    Originally Posted by DevCoach
    For completeness here is the version that uses the csv module to read the input too - this will correctly handle fields that are in quotes:

    Code:
    import csv
    input = csv.reader(open('my/input/file.txt'), 'excel-tab')
    output = csv.writer(open('the/output/file.csv', 'w'))
    
    for fields in input:
       addr = "%s@%s" % fields[1], fields[0]
       output.writerow( [addr, 'MailServerName', addr, fields[4], "YES", "YES", "NO"] )

    Can anybody show me how to capture the input directly from an HTML form instead of reading it from a directory? I'm basically doing the same concept above except my input file will come from an HTML form...

    Thanks in advance!
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Posts
    89
    Rep Power
    13
    Originally Posted by zci
    Can anybody show me how to capture the input directly from an HTML form instead of reading it from a directory? I'm basically doing the same concept above except my input file will come from an HTML form...

    Thanks in advance!
    if you mean grabbing input via CGI from an HTML form than you should read the devshed tutorial

    http://www.devshed.com/c/a/Python/Wr...ams-in-Python/

IMN logo majestic logo threadwatch logo seochat tools logo