2015-11-22 257 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並因此是錯誤。