Import question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kippy
    New Member
    • Jul 2007
    • 2

    Import question

    Hello,

    I am very, very new at this, new at programming in general actually. This is probably a very simple question, but I need to start somewhere.

    I am trying to use a python script as part of the HEC-DSSVue software package to write and extract from the DSS database. I need to read in a csv file to then write to the database.

    The HEC-DSSVue software includes a simple python script editor. If my understanding is correct, I need to import the csv module. I am using:

    Code:
    import csv
    This yields a ImportError: no module named csv.

    Other script imports have been:

    Code:
    from hec.script import*
    I am just guessing that I need to set something up in the CMD file, but I am not sure what it should be. Any help would be very much appreciated.

    Thanks.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by kippy
    Hello,

    I am very, very new at this, new at programming in general actually. This is probably a very simple question, but I need to start somewhere.

    I am trying to use a python script as part of the HEC-DSSVue software package to write and extract from the DSS database. I need to read in a csv file to then write to the database.

    The HEC-DSSVue software includes a simple python script editor. If my understanding is correct, I need to import the csv module. I am using:

    Code:
    import csv
    This yields a ImportError: no module named csv.

    Other script imports have been:

    Code:
    from hec.script import*
    I am just guessing that I need to set something up in the CMD file, but I am not sure what it should be. Any help would be very much appreciated.

    Thanks.
    Have you installed Python on your system? Does the file csv.py exist on your system. Check your path:
    Code:
    import sys
    for p in sys.path:
        print p
    If the file exists in one of these subdirectories and the subdirectory has an __init__.py file, it should import.

    If the CSV file is simple, you may be able to write your own. E.G. this simple file:
    1 01,02,05,07,18
    2 00,01,04
    3 09,20,21
    4 12,34,56,77,88, 99
    5 03,05,77,88,54
    The following code will create a dictionary from which you can write to your database:[code=Python]def file_data1(f):
    dd = {}
    for line in f:
    lineList = line.split()
    dd[int(lineList[0])] = [int(i) for i in lineList[1].split(',')]
    return dd

    dd = file_data1(open ('csv.txt').rea dlines())[/code]

    Comment

    Working...