【问题标题】:CSV reader issue [duplicate]CSV阅读器问题[重复]
【发布时间】:2015-11-22 20:42:41
【问题描述】:

您好,我正在尝试将 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')

【问题讨论】:

    标签: python-2.7


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

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

      更多信息在这里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()
      

      【讨论】:

        【解决方案3】:

        简短回答

        在引用文件名时使用原始字符串,例如 r'C:\FrasersSlipReconstruction\Scripts\vector.csv' 或更好的是 os.path.join。

        \v 是垂直制表符的换页符。这就是解释器将其解析为 \x0b 从而导致错误的原因。

        【讨论】:

          猜你喜欢
          • 2015-12-16
          • 2018-10-04
          • 1970-01-01
          • 2014-10-31
          • 1970-01-01
          • 2021-05-19
          • 2011-08-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多