简体   繁体   English

Python输出到命令行; 错误未定义

[英]Python output to command line; Error not defined

I am trying to write a script that takes one argument and writes the output to the command window. 我正在尝试编写一个脚本,该脚本接受一个参数并将输出写入命令窗口。 For some reason I am getting the error: 出于某种原因,我收到错误:

NameError: name 'month' not defined

Here is the entire script: 这是整个脚本:

import sys


hex = str(sys.argv)
sys.stdout.write (month(hex) + " " + day(hex) + ", " + year(hex) + " " + hour(hex) + ":" + minute(hex) + ":" + second(hex))

def year (hex):
    year = int(hex[0:2], 16)
    year = year + 1970
    return str(year)

def month (hex):
    month = int(hex[2:4], 16)
    if month == 0:
        month = "January"
        return month
    elif month == 1:
        month = "February"
        return month
    elif month == 2:
        month = "March"
        return month
    elif month == 3:
        month = "April"
        return month
    elif month == 4:
        month = "May"
        return month
    elif month == 5:
        month = "June"
        return month
    elif month == 6:
        month = "July"
        return month
    elif month == 7:
        month = "August"
        return month
    elif month == 8:
        month = "September"
        return month
    elif month == 9:
        month = "October"
        return month
    elif month == 10:
        month = "November"
        return month
    else:
        month = "December"
        return month

def day (hex):
    day = int(hex[4:6], 16)
    return str(day)

def hour (hex):
    hour = int(hex[6:8], 16)
    if hour < 10:
        return "0" + str(hour)
    else:
        return str(hour)

def minute (hex):
    minute = int(hex[8:10], 16)
    if minute < 10:
        return "0" + str(minute)
    else:
        return str(minute)

def second (hex):
    second = int(hex[10:12], 16)
    if minute < 10:
        return "0" + str(second)
    else:
        return str(second)

When I used an online python interpreter to run it, the functions worked fine. 当我使用在线python解释器来运行它时,功能运行良好。 I just don't know how to run it from the command line and send the output back to the command window. 我只是不知道如何从命令行运行它并将输出发送回命令窗口。 Thanks 谢谢

Put the line sys.stdout.write ... after your function definitions. 在函数定义之后放置sys.stdout.write行。

Please, don't use month for both your function and a variable inside this function. 请不要在函数内使用month和函数中的变量。

In python a file is parsed line by line from top to bottom, so the functions month , year , hour , minute and second are not defined yet for this line: 在python中,文件从上到下逐行解析,因此该行的monthyearhourminutesecond函数尚未定义:

sys.stdout.write (month(hex) + " " + day(hex) + ", " + year(hex) + " " + hour(hex) + ":" + minute(hex) + ":" + second(hex))

Move these function definitions above this line. 将这些函数定义移到此行之上。

And using a local variable with same name as the function name is not a good idea. 使用与函数名称同名的局部变量不是一个好主意。

As sys.argv returns a list (with first element being the filename), so you can't apply hex over it. 由于sys.argv返回一个列表(第一个元素是文件名),因此您不能在其上应用hex Apply hex on the items of the list, ie hex( int(sys.argv[1]) ) 在列表的项目上应用hex ,即hex( int(sys.argv[1]) )

>>> lis = ['foo.py', '12']
>>> hex( int(lis[1]) )    #use `int()` as hex expects a number
'0xc'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM