我需要在Python的切片表示法上有一个很好的解释(引用是一个加号).
对我来说,这种符号需要一点点提升.
它看起来非常强大,但我还没有完全了解它.
我原来是一名C程序员.我已经看过许多技巧和"黑客"来阅读许多不同的论点.
Python程序员可以通过哪些方式实现这一目标?
我遇到了以下Python代码部分的问题:
# Open/Create the output file
with open(sys.argv[1] + '/Concatenated.csv', 'w+') as outfile:
try:
with open(sys.argv[1] + '/MatrixHeader.csv') as headerfile:
for line in headerfile:
outfile.write(line + '\n')
except:
print 'No Header File'
Run Code Online (Sandbox Code Playgroud)
具体来说错误如下:
Traceback (most recent call last): File "ConcatenateFiles.py", line 12, in <module> with open(sys.argv[1] + 'Concatenated.csv', 'w+') as outfile:
IndexError: list index out of range
Run Code Online (Sandbox Code Playgroud)
我做了一些研究,似乎sys.argv
在运行脚本时可能需要在命令行进行参数,但我不确定要添加什么或问题可能是什么!我也搜索了网站,但我发现的所有解决方案都没有评论和/或不包括我的开放功能.
任何帮助是极大的赞赏.
我有两台带有Windows的计算机,我刚刚在其中一台计算机上发现,如果我直接运行python代码,例如:
test_args.py input1 input2
Run Code Online (Sandbox Code Playgroud)
Python不会识别我给出的输入,但这有效:
python test_args.py input1 input2
Run Code Online (Sandbox Code Playgroud)
我试过这段代码:
import sys
print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)
Run Code Online (Sandbox Code Playgroud)
第一种方式(test_args.py)返回:
Number of arguments: 1 arguments.
Argument List: ['D:\\Test\\args\\test_args.py']
Run Code Online (Sandbox Code Playgroud)
虽然sceond方式(python test_args.py input1 input2)返回:
Number of arguments: 3 arguments.
Argument List: ['D:\\Test\\args\\test_args.py', 'input1', 'input2']
Run Code Online (Sandbox Code Playgroud)
关于这可能发生什么的任何想法?此问题仅发生在我的一台计算机上,两台都有相同版本的Windows.
谢谢!
我在regedit关键字"python"中搜索,发现在"C:\ Python27\python.exe""%1"之后缺少%*的两个键:
电脑\ HKEY_CLASSES_ROOT \应用程序\ python.exe
电脑\ HKEY_CLASSES_ROOT\py_auto_file \壳\开放\命令
并且.py与py_auto_file相关联,即使我尝试了关联.py Python.File
更改两个键解决了这个问题,谢谢!
我正在编写一个简单的Python客户端和服务器,它可以很好地在我的代码中传递服务器地址,但是,我希望用户能够输入服务器地址并抛出错误,如果它不正确.当我有下面的代码时,我收到来自终端"列表索引超出范围"的错误消息.
server = (sys.argv[1])
serverAdd = (server, '65652') # server address and port number
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我这个.
当我在python中运行我的客户端程序时,我希望能够输入一个地址来连接并将其存储在服务器中.我通过输入programname.py直接从命令行运行程序.服务器已在运行侦听传入连接.
我可以在运行时传递命令行参数
python <filename>.py arg1
Run Code Online (Sandbox Code Playgroud)
但是当我尝试传递运行 pytest 的命令行参数时,它失败并给出如下错误。你能给些建议么。
pytest <filename>.py arg1
ERROR: file not found: arg1
Run Code Online (Sandbox Code Playgroud)
编辑:
例如,我正在考虑以这种方式使用它,假设我已经传递了一个参数并且正在通过 sys.argv 读取它:
import sys
arg = sys.argv[3]
def f():
return 3
def test_function():
assert f() == arg
Run Code Online (Sandbox Code Playgroud) 我目前正在学习Python的艰难之路.我想这个例子可能已经过时了,所以我想在这里得到反馈.
我正在使用Python 3.1
from sys import argv
script, first, second, third = argv
print("the script is called:", (script))
print("your first variable is:", (first))
print("your second variable is:", (second))
print("your third variable is:", (third))
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
Traceback (most recent call last):
File "/path/ch13.py", line 3, in <module>
script, first, second, third, bacon = argv
ValueError: need more than 1 value to unpack
Run Code Online (Sandbox Code Playgroud)
知道什么是错的吗?
我在编码挑战中停靠点,指定我需要从STDIN读取.这是我的输入法:
def __init__(self, input):
self._dictionary = {}
with open(input, 'r') as f:
reader = csv.reader(f, delimiter='\t')
for row in reader:
if self._dictionary.__contains__(row[0]):
self._dictionary[row[0]].append(row[1])
else:
self._dictionary.update({row[0]: row[1].split()})
Run Code Online (Sandbox Code Playgroud)
并在脚本的末尾
if __name__ == "__main__":
script = Script(sys.argv[1])
for line in script.output_method():
print line
Run Code Online (Sandbox Code Playgroud)
在要求从标准输入读取的挑战中使用sys.argv是错误的吗?有什么不同?我应该怎样做才能满足要求?
python ×9
argv ×1
cmd ×1
command-line ×1
iterable ×1
list ×1
python-3.x ×1
slice ×1
windows ×1