Python格式化脚本适用于OSX,但在Windows上失败

时间:2016-09-16 15:22:16

标签: python windows macos powershell

所以我得到了一个脚本,它将从服务器powershell命令发出的文件格式化为选项卡式分隔文本文件,当我在同一个test.txt文件上运行它时,OSx上的一切顺利但是当我尝试在Windows服务器上运行它时它输出一个空的错误名称文件。我没有得到任何错误,但它显然在两个操作系统上都不起作用。 这是脚本:

from sys import argv
import time
import datetime

script, filename = argv

optionNames = ['Client ID', 'User Name', 'Computer Name', 'Ext Privilege', 'IP Address', 'MAC Address', 'Connect Time', 'Duration', 'App Version', 'App Language', 'User Connections', 'License', 'File Name', 'Account Name', 'Privilege Set']

nbOptions = len(optionNames)
optionsFirstCharIndex = []
myFile = open(filename)
myText = myFile.readlines()


#print myText (debug)
for i in range(0,nbOptions):
    optionsFirstCharIndex.append(myText[0].find(optionNames[i]))

charIndexSize = len(optionsFirstCharIndex) - 1
storableOptionsLines = []
#store each as list item
for n in range(0,len(myText)):
    storableOptions = []
    for i in range(0,charIndexSize+1):
        if i != charIndexSize:
            j = optionsFirstCharIndex[i]
            k = optionsFirstCharIndex[i+1]
            storableOptions.append(myText[n][j:k].rstrip())
        else:
            j = optionsFirstCharIndex[-1]
            storableOptions.append(myText[n][j:].rstrip())
    storableOptionsLines.append(storableOptions)

outputText = ""
# formating the text 
for line in storableOptionsLines:
    for word in line:
        outputText += word + "\t"
    outputText += "\n"
s = time.time()
tStamp = datetime.datetime.fromtimestamp(s).strftime("%Y-%m-%d_%H:%M")

with open("report_" + tStamp + ".txt", 'w') as myOutputFile:
    myOutputFile.write(outputText)
myOutputFile.close()

当我使用打印调试时,我确实得到一个列表,其中包含OSx源文件中的每一行,但在Windows中,我得到一个包含大量x00,x00U,x00F等的超长列表。最后一个字母似乎是每个字的一封信。 \ xfeC \ x00l \ x00i \ x00e \ x00n \ x00t代表客户端。有没有人知道发生了什么? 非常感谢!

0 个答案:

没有答案