将元素附加到数组实例变量并打印出来 - Python

时间:2014-02-07 17:16:34

标签: python arrays append instance-variables

我正在尝试读取文件并提取记录并在类中打印文件。我得到的错误是该对象没有附加函数?我已将其声明为数组,但似乎无法识别它。任何暗示问题是什么?这是解决问题的有效方法吗?

import os 

class URL():
    Test=[]
    def read(self,file):
        for l in open(file):
            fields=l.split(',')
            company=fields[1].replace(" ",'+')
            adress="+".join((str(fields[5]),str(fields[11]) ) )
            self.Test.append( "".join(("http://www.someurl/market-search?q=",company)))
    def Print(self):
        for i in Test:
            return i 

ROOT = os.getcwd()
START_URL=URL()
START_URL.read(ROOT+'\Company_Lists\Test_of_company.csv')

print START_URL.Print

1 个答案:

答案 0 :(得分:1)

我会改写这个:

import os 
import os.path


class URL(object):
    Test = []
    def read(self, filename):
        with open(filename) as f:
            for line in f:
                fields = line.split(',')
                company = fields[1].replace(" ", '+')
                self.Test.append("http://www.someurl/market-search?q={0}".format(company))

    def print(self):
        for i in self.Test:
            print i 


def main():
    root = os.getcwd()
    start_url = URL()
    p = os.path.join(root, 'Company_Lists', 'Test_of_company.csv')
    start_url.read(p)
    start_url.print()


if __name__ == '__main__':
    main()