Python:将参数传递给脚本 [英] Python: pass arguments to a script
本文介绍了Python:将参数传递给脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个工作代码可以从csv列中打印随机行.
I have a working code to print random lines from a csv column.
#!/usr/bin/python
import csv
import random
**col**=2
with open('<filename>','r') as f:
reader=csv.reader(f)
data=[row[col] for row in reader]
from random import shuffle
shuffle(data)
print '\n'.join(data[:**100**])
f.close();
我想将此脚本参数化.通过,位置(col)&样本(例如100个值)
I want to paramaterize this script. by passing , position (col) & sample (e.g. 100 values)
我找不到合适的教程来解释这一点.有什么想法吗?
I am unable to find a decent tutorial which would explain this. any thoughts ?
推荐答案
您可以像这样使用sys模块将命令行参数传递给python脚本.
You can use the sys module like this to pass command line arguments to your python script.
import sys
name_of_script = sys.argv[0]
position = sys.argv[1]
sample = sys.argv[2]
然后您的命令行将是...
and then your command line would be...
./myscript.py 10 100
./myscript.py 10 100
这篇关于Python:将参数传递给脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文