通过循环进行第二次迭代时出现“熊猫”错误

时间:2019-01-02 21:53:18

标签: python pandas dictionary python-docx

在嵌套的for循环的第二次迭代中,我得到了错误:

  

“文件   “ C:\ Users \ SmithC113 \ PycharmProjects \ ASCII_Word \ venv \ lib \ site-packages \ pandas \ io \ formats \ format.py”,   1404行,在       返回(len(non_na)> 0,并且所有(在non_na中x的所有(x.endswith('0')为x)))和SystemError:错误返回而未设置异常。”

我不知道是什么导致了此错误。任何帮助表示赞赏。

我尝试了dict.clear,可能是字典只是自动追加数组中的数据,但这没有用。我还尝试了dict.update以及是否有dict.clear。我不明白该错误,因此很难找到解决方案。

import tkinter as tk
from tkinter.filedialog import askopenfilename
import pandas as pd  # Pandas - gives better table display results

from docx import Document  # Invokes Document command from docx
from docx.shared import Pt  # This makes it possible to dictate the font     size we are writing to the doc-x

# IMPORT ASCII CP DATA
root = tk.Tk()
root.withdraw()
file_path = askopenfilename(title="Choose Control File")  # returns the         file path as variable for future use
data = pd.read_csv(file_path, sep=",", header=None)
r = len(data[1])  # number of rows in ASCII file (aka number of 3D points)
print('Number of CP Points imported =', r)  # This is check for now

# IMPORT ASCII BM DATA
file_pathBM = askopenfilename(title='Choose Benchmark File')  # returns the         file path as variable for future use
data_bm = pd.read_csv(file_pathBM, sep=",", header=None)
rbm = len(data_bm[1])  # number of rows in ASCII file (aka number of 3D         points)
    print('Number of BM Points imported =', rbm)  # This is check for now

# IMPORT WORD DOCUMENT
root = tk.Tk()
root.withdraw()
doc_path = askopenfilename(title="Choose Word File")  # returns the file         path as variable for future use
document = Document(doc_path)  # Imports Word Document to Modify
t = len(document.paragraphs)  # gives the number of lines in document
print('Total Number of lines =', t)  # this is a check for now
font = document.styles['Normal'].font
font.name = 'Arial'
font.size = Pt(8)
x = 0

for paragraph in document.paragraphs:
    data_dict = {
        'NORTHING:': data[1][x],
        'EASTING:': data[2][x],
        'ELEV:': data[3][x],
        'CSF:': data[8][x],
        'STD. DEV.:': 'N: {0}\t E: {1}\t EL: {2}'.format(data[5][x], data[6][x], data[7][x])
    }
    for k, v in data_dict.items():
        if k in paragraph.text:
            paragraph.clear()
            run = paragraph.add_run()
            run.text = k + '\t'
            run.font.bold = True
            run.font.underline = True
            run = paragraph.add_run()
            run.text = '{}'.format(v)
    x += 1
    data_dict.clear()

for paragraph in document.paragraphs:
    print(paragraph.text)  # Prints the text in the entire document
# document.save('test1_save.docx') #Saves as Word Document after Modification

在调试器中,我可以看到字典值像我希望的那样被覆盖,但是在第二次迭代的if语句中,出现上述错误。我感到困惑,因为数据与第一次迭代的类型相同,而字典是单值数据[y] [x]。另外,您将需要熊猫,python-docx和numpy。

来自ASCII文件的数据的伪装为:

1,126081.2245,12608965.8380,691.5940,CP,0.0053,0.0036,0.0006,1.0000470038 2,126327.6401,12609040.7808,692.2263,CP,0.0041,0.0043,0.0009,1.0000467574 3,126738.8659,12609037.5712,691.4403,CP,0.0043,0.0058,0.0030,1.0000464372 4,127066.6430,12608990.3587,690.2438,CP,0.0051,0.0050,0.0011,1.0000462105 5,127303.7734,12609061.0079,692.1387,CP,0.0030,0.0053,0.0029,1.0000459125

将其粘贴到文本文件中,并用于两次导入

Word文档格式如下:

  

CP1

     

说明:在西北设置带有MDOT帽的5/8“ X 36”铁杆   主要街道和M-51街区。

     

证人:

     
      
  1. 消防栓S42W 30.5'

  2.   
  3. LIGHT POLE N08E 27.0'

  4.   
  5. LIGHT POLE N47E 70.0’

  6.   
  7. 渔获盆地中心S45W 24.0”

  8.   
     

其他:

     

轻松:

     

ELEV:

     

CSF:

     

STD。 DEV .: N:E:ELEV:

     

站:

     

偏移:

     

CP2

     

说明:在东南设置带有MDOT帽的5/8“ X 36”铁杆   SYCAMORE大街和M-51的象限。

     

证人:

     
      
  1. 暴风雨S73W 10.0'

  2.   
  3. CATCH BASIN N0W 45.0’

  4.   
  5. STRAIN POLE S53W 20.5'

  6.   
  7. FENCE CORNER S43W 11.5'

  8.   
     

其他:

     

轻松:

     

ELEV:

     

CSF:

     

STD。 DEV .: N:E:ELEV:

     

站:

     

偏移:

0 个答案:

没有答案