Mac 上的 Python : error during reading from numbers file

标签 python macos python-2.7

我想从我的数字表中读取一些数据。第一行是“标题”行,包含每列的名称,每隔一行包含数据。 我用 python 编写了一个简单的程序来阅读此内容,但是每次运行此代码时我都会得到:

File "dataReader.py", line 11, in <module>
    for row in fileReader:
_csv.Error: line contains NULL byte

我试图对每一行进行编码,但没有帮助。 下面我传递了我的代码(在这一代码中,我没有调用 utf_8_encoder 函数,但即使我尝试使用它 - 我仍然有错误)。 我想从文件中逐行读取,并将其作为字符串读取。 我怎样才能做到这一点?

import csv
import codecs

def utf_8_encoder(unicode_csv_data):
    for line in unicode_csv_data:
        yield line.encode('utf-8')

if __name__ == "__main__":
    dataFile = open("file.numbers", 'rU')
    fileReader = csv.reader(dataFile)
    for row in fileReader:
        print row 

这也是我的文件的示例: enter image description here

最佳答案

您已将文件另存为 .numbers 文件,而不是 CSV 文件。 csv 模块用于读取 CSV(逗号分隔值)文件,无法解释 .numbers 文件。

要使您的程序正常运行,请将数据保存为 CSV 文件。

关于Mac 上的 Python : error during reading from numbers file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29710277/

相关文章:

python - 插值等距点

python - PySpark groupby 多个时间窗口

macos - 在 El Capitan 中隐藏图标聚光灯

bash - OSX 恶意终端命令(冒号、括号、大括号、apersand 等)

python - 解释打印 test_list

python - self[] 在方法中是什么意思?

linux - virtualenv 在新的 bash shell 中没有被激活

python - 使用Python发送短信

python - 如何在anaconda中安装包?

python - python 脚本的采购输出因管道异常异常而失败