|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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"] )
|
|
#4
|
|||
|
|||
|
Thank you very much, I am sure this will prove to be invaluable.
|
|
#5
|
|||
|
|||
|
New question, is there any way to stop or start a windows service through Python?
|
|
#6
|
|||
|
|||
|
Quote:
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 |
|
#7
|
|||
|
|||
|
Quote:
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! |
|
#8
|
|||
|
|||
|
Quote:
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/ |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Noob script help. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|