CSV reader issue

Question:

Hi I am trying to read a csv file into an array. The file is called vector.csv.
For some reason the interpreter is changing the file name to x0bector.csv and the script fails. See OUTPUT:
If I change the file name for example to cords.csv script runs fine. Does anyone know what’s going on here?

# Common Standard Python modules.
import sys
import os
import string
import csv

# Common MineSight Grail modules. 
from grail import gsys
from grail.data import geometry

def read_csv(filepath):
    f = open(filepath, 'r')
    vector = []
    csv_f = csv.reader(f)
    for row in csv_f:
        vector.append(row)
    return vector

def get_polylines(geometry):
    count = geometry.getpolylinecount()
    points = []
    for index in range(count): 
        points.append(geometry.getpolylinepointsat(index))

    return points


def run_script():
    """Added the following print statement for testing"""    
    print "Processing ... OK."  
    g1 = geometry.Geometry("C:FrasersSlipReconstructionScriptsFRWCrest_Before.msr")
    points = get_polylines(g1)

    vectors = read_csv("C:FrasersSlipReconstructionScriptsvector.csv")
    print vectors

# These two lines ensure that our script will execute the "run_script"
# routine from both MS3D and the command line.
gmain = gsys.GMAIN(run_script, __name__)
gmain.run()

OUTPUT:

Python Error (14):
Traceback (most recent call last):
  File "[minesight-9.50.05b66813-51]/grail/_script.py", line 136, in gmain
  File "[minesight-9.50.05b66813-51]/grail/gsys.py", line 167, in __call__
  File "C:FrasersSlipReconstructionScriptstransform.py", line 34, in run_script
    vectors = read_csv("C:FrasersSlipReconstructionScriptsvector.csv")
  File "C:FrasersSlipReconstructionScriptstransform.py", line 12, in read_csv
    f = open(filepath, 'r')
IOError: (22, "invalid mode ('rb') or filename", 'C:\FrasersSlipReconstruction\Scriptsx0bector.csv')
Asked By: ozzyzig

||

Answers:

On Windows use the r prefix for pasted in files names:

r"C:FrasersSlipReconstructionScriptsvector.csv"

The v has a special meaning, it is a vertical tab. You can escape its meaning by doubling the back slash \ or using a raw string by prefixing the path name with r.

Answered By: Mike Müller

your file path needs to have the ‘s escaped

more info here https://docs.python.org/2.0/ref/strings.html

You can use “C:\FrasersSlipReconstruction\Scripts\vector.csv”

# Common Standard Python modules.
import sys
import os
import string
import csv

# Common MineSight Grail modules. 
from grail import gsys
from grail.data import geometry

def read_csv(filepath):
    f = open(filepath, 'r')
    vector = []
    csv_f = csv.reader(f)
    for row in csv_f:
        vector.append(row)
    return vector

def get_polylines(geometry):
    count = geometry.getpolylinecount()
    points = []
    for index in range(count): 
        points.append(geometry.getpolylinepointsat(index))

    return points


def run_script():
    """Added the following print statement for testing"""    
    print "Processing ... OK."  
    g1 = geometry.Geometry("C:\FrasersSlipReconstruction\Scripts\FRWCrest_Before.msr")
    points = get_polylines(g1)

    vectors = read_csv("C:\FrasersSlipReconstruction\Scripts\vector.csv")
    print vectors

# These two lines ensure that our script will execute the "run_script"
# routine from both MS3D and the command line.
gmain = gsys.GMAIN(run_script, __name__)
gmain.run()
Answered By: Daniel Stewart

SHORT ANSWER

Use raw string such as r’C:FrasersSlipReconstructionScriptsvector.csv’ or better still, os.path.join when referring to the file names.

v is a form feed for vertical tab. That is why the interpreter parses it as x0b and hence the error.

Answered By: Ujjwal Aryan
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.