[Python] next(funzione) in python3 ma in python2?

Marco Ippolito ippolito.marco a gmail.com
Lun 2 Mar 2015 06:53:25 CET


Buon giorno a tutti,

il seguente pezzo di codice in python3 mi funziona, mentre i miei vari
tentativi di fare la stessa cosa in python2, sono andati a vuoto:

#!/usr/bin/python3
import sqlite3

class Sqlite3Extract:
    def __init__(self, db_name):
        self.db_name = db_name
        self.con = sqlite3.connect(self.db_name)
        self.con.isolation_level = None
        self.url_canonicalization = ""

    def ans_y(self, answer):
        for x in enumerate(answer):
            yield(x)

    def sqlite3_extraction_meth(self):
        self.cur = self.con.cursor()
        self.cur.execute("SELECT DISTINCT link FROM link;")
        ris = self.cur.fetchall()
        set_ris = set(ris)
        aaa = self.ans_y(set_ris)

        url_to_canonicalize = next(aaa)[1][0] # python3
        return url_to_canonicalize

        self.ans_y(ris).close()

    def get_sqlite3_content(self):
        return self.sqlite3_extraction_meth()


if __name__ == '__main__':
    nome_database = 'xyz.db'
    sqlite3_extract = Sqlite3Extract(nome_database)
    coll = sqlite3_extract.get_sqlite3_content()
    print(coll)

eseguendo questo codice ottengo il risultato voluto:
./sqlite3Extraction.py
https://www.machinetools.com/en/companies/54736-balance-systems-srl
./sqlite3Extraction.py
http://www.balancesystems.it.ipaddress.com/
......

mentre con python2 non mi funziona:

#!/usr/bin/python

import sqlite3

class Sqlite3Extract:
    def __init__(self, db_name):
        self.db_name = db_name
        self.con = sqlite3.connect(self.db_name)
        self.con.isolation_level = None
        self.url_canonicalization = ""

    def ans_y(self, answer):
        for x in enumerate(answer):
            yield(x)

    def sqlite3_extraction_meth(self):
        self.cur = self.con.cursor()
        self.cur.execute("SELECT DISTINCT link FROM link;")
        ris = self.cur.fetchall()
        set_ris = set(ris)
        aaa = self.ans_y(set_ris)

        return aaa.next()[1][0]

        self.ans_y(ris).close()

    def get_sqlite3_content(self):
        return self.sqlite3_extraction_meth()


if __name__ == '__main__':

    nome_database = 'xyz.db'
    sqlite3_extract = Sqlite3Extract(nome_database)
    coll = sqlite3_extract.get_sqlite3_content()
    print(coll)

./sqlite3Extraction.py
http://investing.businessweek.com/research/stocks/private/people.asp?privcapId=47477918

./sqlite3Extraction.py
http://investing.businessweek.com/research/stocks/private/people.asp?privcapId=47477918

./sqlite3Extraction.py
http://investing.businessweek.com/research/stocks/private/people.asp?privcapId=47477918

In cosa sbaglio in python2?


Maggiori informazioni sulla lista Python