2015-11-22 220 views
0

嗨我想读取一个csv文件到数组中。该文件被称为vector.csv。 由于某种原因,解释器正在将文件名更改为x0bector.csv,并且脚本失败。请参阅OUTPUT: 如果我将文件名更改为cords.csv脚本运行正常。有人知道这里发生了什么吗?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() 

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:\FrasersSlipReconstruction\Scripts\transform.py", line 34, in run_script 
    vectors = read_csv("C:\FrasersSlipReconstruction\Scripts\vector.csv") 
    File "C:\FrasersSlipReconstruction\Scripts\transform.py", line 12, in read_csv 
    f = open(filepath, 'r') 
IOError: (22, "invalid mode ('rb') or filename", 'C:\\FrasersSlipReconstruction\\Scripts\x0bector.csv') 

回答

2

在Windows上使用r前缀的文件名粘贴:

r"C:\FrasersSlipReconstruction\Scripts\vector.csv" 

\v有特殊的意义,它是一个垂直的标签。您可以通过加倍反斜杠\\或通过在路径名前加上r来使用原始字符串来逃避其含义。

+0

不错的工作感谢:) – ozzyzig

0

您的文件路径需要有\的转义

更多的信息在这里https://docs.python.org/2.0/ref/strings.html

您可以使用 “C:\\ \\ FrasersSlipReconstruction脚本\\ 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() 
-1

简短的回答

使用原始的字符串,如R'C:\ FrasersSlipReconstruction \脚本\ vector.csv”或贝特当引用文件名时,仍然是os.path.join。

\ v是垂直制表符的表单提要。这就是为什么解释者将其解析为\ x0b并因此是错误。