您好我正在尝试将csv文件读入数组。该文件名为vector.csv。 由于某种原因,解释器正在将文件名更改为x0bector.csv,并且脚本失败。请参阅输出: 如果我将文件名更改为例如cords.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()
输出:
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')
答案 0 :(得分:2)
在Windows上,使用粘贴在文件名中的r
前缀:
r"C:\FrasersSlipReconstruction\Scripts\vector.csv"
\v
具有特殊含义,它是一个垂直制表符。您可以通过将反斜杠\\
加倍或使用原始字符串(通过在路径名前加r
前缀)来逃避其含义。
答案 1 :(得分:0)
您的文件路径需要进行转义
此处有更多信息https://docs.python.org/2.0/ref/strings.html
您可以使用" 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()
答案 2 :(得分:-1)
简短回答
在引用文件名时,使用原始字符串,如r'C:\ FrasersSlipReconstruction \ Scripts \ vector.csv'或更好,os.path.join。
\ v是垂直标签的换页符。这就是解释器将其解析为\ x0b并因此解析错误的原因。