熊猫和NumPy无法读取整个文件

时间:2019-01-14 20:47:06

标签: python pandas

我正在使用panda.read_table将表格文件收集到矩阵中。但是,panada可以毫无问题地处理行的前4772行,然后按如下所示跳过错误:

return self._getitem_column(key)
  File "C:\Users\YOUJIA LI\PycharmProjects\untitled1\venv\lib\site-packages\pandas\core\frame.py", line 2695, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Users\YOUJIA LI\PycharmProjects\untitled1\venv\lib\site-packages\pandas\core\generic.py", line 2487, in _get_item_cache
    res = cache.get(item)
TypeError: unhashable type: 'slice'

这是我用于处理数据的代码:

 from get_line_number import get_line_number
import numpy as np
import pandas as pd

#import file in python
phrase = "STARTS"
file_name = "C:\\Users\\YOUJIA LI\\Desktop\\program\\java\\LAYER.PLT" #(change path )

#find where the file starts
linenumber = get_line_number(phrase,file_name)
with open(file_name) as myfile:
    lineskip = myfile.readlines()[int(linenumber):]

#create a temp. file to store the numerical data
file = open("token.txt",'w')
with open(file_name) as myfile:
    for _ in range(linenumber):
        next(myfile)
    for line in myfile:
        values = line.split()
        # get ride of string leave num along
        values1 = str(values).replace("[","")
        values1 = values1.replace("]","")
        values1 = values1.replace("'","")

        file.write(values1 +'\n')
myfile.close()
matrix = pd.read_table('token.txt', sep=',',header=None,error_bad_lines=False)
print(matrix)

token.txt的屏幕截图如下: enter image description here

0 个答案:

没有答案