[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: csv script python



Il 22/02/2013 11:00, Francesco Berni ha scritto:
On Fri, 22 Feb 2013 10:02:43 +0100
emmanuel segura<emi2fast@gmail.com>  wrote:

>Grazie Fabrizio
>
>Funziona:-), Grazie 1000 non la sapevo questa cosa
>
non ci avevo fatto caso.
e' dovuto a come python importa le cose, in breve stavi importando il
file stesso dentro se stesso facendo parecchio casino

Sperando di non dire un'estrema stupidaggine:

--File script: csv.py------------------

#!/usr/bin/python
import csv

fd = open("test.csv","rb")
csv = csv.reader(fd, dialect='excel')

for row in cr:
        print ','.join(row)
--------------------------------------

l'import dovrebbe prevalere per csv.py, che nella cartella
è unico, quindi lo script importa alla fine se stesso.

Non essendo implementato il metodo .reader (visto che non
è stato implementato nello script) va in errore.

AttributeError: 'module' object has no attribute 'reader'

Nella documentazione infatti si legge (6.11. The import statement) [1]:

Import statements are executed in two steps: (1) find a module, and initialize it if necessary; (2) define a name or names in the local namespace (of the scope where the import statement occurs). The statement comes in two forms differing on whether it uses the from keyword. The first form (without from) repeats these steps for each identifier in the list. The form with from performs step (1) once, and then performs step (2) repeatedly. For a reference implementation of step (1), see the importlib module.

To understand how step (1) occurs, one must first understand how Python handles hierarchical naming of modules. To help organize modules and provide a hierarchy in naming, Python has a concept of packages. A package can contain other packages and modules while modules cannot contain other modules or packages. From a file system perspective, packages are directories and modules are files. The original specification for packages is still available to read, although minor details have changed since the writing of that document.

*Once the name of the module is known (unless otherwise specified, the term “module” will refer to both packages and modules), searching for the module or package can begin. The first place checked is sys.modules, the cache of all modules that have been imported previously. If the module is found there then it is used in step (2) of import.*


Dario


-- Link --

[1] http://docs.python.org/3.1/reference/simple_stmts.html






Reply to: