Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 23rd, 2004, 03:17 AM
Zaph0d042 Zaph0d042 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 3 Zaph0d042 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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

Reply With Quote
  #2  
Old October 23rd, 2004, 07:10 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,254 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 8 h 9 m
Reputation Power: 265
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

Reply With Quote
  #3  
Old October 23rd, 2004, 07:26 AM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,254 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 8 h 9 m
Reputation Power: 265
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"] )

Reply With Quote
  #4  
Old October 23rd, 2004, 01:19 PM
Zaph0d042 Zaph0d042 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 3 Zaph0d042 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thank you very much, I am sure this will prove to be invaluable.

Reply With Quote
  #5  
Old October 23rd, 2004, 01:46 PM
Zaph0d042 Zaph0d042 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2004
Posts: 3 Zaph0d042 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
New question, is there any way to stop or start a windows service through Python?

Reply With Quote
  #6  
Old October 23rd, 2004, 02:04 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,254 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 8 h 9 m
Reputation Power: 265
Quote:
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

Reply With Quote
  #7  
Old December 3rd, 2004, 01:18 PM
zci zci is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 17 zci User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 58 m 31 sec
Reputation Power: 0
Quote:
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!

Reply With Quote
  #8  
Old December 4th, 2004, 04:59 PM
jimmy2k1 jimmy2k1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 89 jimmy2k1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 48 m 50 sec
Reputation Power: 7
Quote:
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/W...rams-in-Python/

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Noob script help.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT