所以我用Python脚本编写了一些类,例如:
#!/usr/bin/python
import sys
import csv
filepath = sys.argv[1]
class test(object):
def __init__(self, filepath):
self.filepath = filepath
def method(self):
list = []
with open(self.filepath, "r") as table:
reader = csv.reader(table, delimiter="\t")
for line in reader:
list.append[line]
如果从命令行调用此脚本,该如何调用method?因此通常我输入:$ python test.py test_file现在,我只需要知道如何访问称为“方法”的类函数即可。
您将创建该类的实例,然后调用该方法:
test_instance = test(filepath)
test_instance.method()
请注意,在Python你不具备创建类只是运行代码。您可以在这里使用一个简单的函数:
import sys
import csv
def read_csv(filepath):
list = []
with open(self.filepath, "r") as table:
reader = csv.reader(table, delimiter="\t")
for line in reader:
list.append[line]
if __name__ == '__main__':
read_csv(sys.argv[1])
我在这里将函数调用移到了__main__
后卫,以便您还可以将该脚本用作模块,并导入该read_csv()
函数以在其他地方使用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句