【问题标题】:How to get past this valueError: Unrecognized character O in format string?如何通过这个 valueError: Unrecognized character O in format string?
【发布时间】:2019-09-26 03:28:36
【问题描述】:

我正在编写的代码从 excel 文件中提取信息。记下名称并将其保存以创建与列名同名的数组。然后它计算列表中“空”字符的数量并删除这些空值。然后它打印要绘制的数据列的长度并创建另一个数组,其中 x 轴值的范围从 1 到 len(数据列)。之后,它会创建一个绘图并将其移动到所需的文件夹。

home = "C:\\Users\\Ishita\\Desktop\\Thesis2\\Raw Data\\Before merger\\"

def count_null(list1, num):
    count = 0
    for element in list1:
        if(element == num):
            count = count + 1
    print("count of null is: ", count)
    return count

def create_text_file(file_col_write, file_col):
    with open(file_col_write, "w") as f:
        for item in file_col:
            f.write(str(item) + "\n")
def plot_and_move(file_col_copy, file_col_write, location_save):
    location_save_plot = home + name_of_file + file_col_copy + ".png"
    plt.plot(x, file_col_copy, color="green")
    plt.xlabel("Days")
    plt.ylabel("Price")
    plt.savefig(location_save_plot, bbox_inches="tight")
    if os.path.exists(location_save_plot):
        print("File exists")
        if os.path.exists(location_save):
            print("Folder exists")
            shutil.move(location_save_plot, location_save)
            shutil.move(file_col_write, location_save)
        else:
            os.makedirs(location_save)
            print("Folder created")
            shutil.move(location_save_plot, location_save)
            shutil.move(file_col_write, location_save)
    plt.show()  
def create_array(file_col, j, file_col_copy):
      file_col = []
      for i in range(1, sheet.nrows):
          file_col.append(sheet.cell_value(i,j))
      #print(file_col)
      for i in range(count_null(file_col, "null")):
          file_col.remove("null")
      #print(file_col)
      file_col_write = home + file_col_copy + ".txt"
      create_text_file(file_col_write, file_col)
      print("file_col length: ",len(file_col))
      file_col_length = len(file_col)-1
      return file_col_length

import xlrd
import matplotlib.pyplot as plt
import os
import shutil


location = home + "testfile-532276.BO.xls"
name_of_file = location.split("\\")[-1]
name_of_file = name_of_file.split(".")[0] + name_of_file.split(".")[1]
print(name_of_file)

wb = xlrd.open_workbook(location) 
sheet = wb.sheet_by_index(0) 

print("Rows:", sheet.nrows)
print("Column:", sheet.ncols)
location_save = home + name_of_file

for j in range(1,5):
    file_col = sheet.cell_value(0,j)
    file_col_copy = sheet.cell_value(0,j)
    print(file_col)
    length = create_array(file_col, j, file_col_copy)    
    x = []
    for i in range(0, length+1):
        x.append(i)
    print("length of x:", len(x))
    #print("x:", x)
    file_col_write = home + file_col_copy + ".txt"
    plot_and_move(file_col_copy, file_col_write, location_save) 

错误信息 Traceback(最近一次调用最后一次):

文件“”,第 1 行,在 runfile('C:/Users/Ishita/Desktop/Thesis2/copy-02.py', wdir='C:/Users/Ishita/Desktop/Thesis2')

运行文件中的文件“C:\Anaconda\lib\site-packages\spyder_kernels\customize\spydercustomize.py”,第 827 行 execfile(文件名,命名空间)

文件“C:\Anaconda\lib\site-packages\spyder_kernels\customize\spydercustomize.py”,第 110 行,在 execfile exec(编译(f.read(),文件名,'exec'),命名空间)

文件“C:/Users/Ishita/Desktop/Thesis2/copy-02.py”,第 86 行,在 plot_and_move(file_col_copy, file_col_write, location_save)

文件“C:/Users/Ishita/Desktop/Thesis2/copy-02.py”,第 25 行,在 plot_and_move plt.plot(x, file_col_copy, color="green")

文件“C:\Anaconda\lib\site-packages\matplotlib\pyplot.py”,第 2789 行,在图中 is not None else {}), **kwargs)

文件“C:\Anaconda\lib\site-packages\matplotlib\axes_axes.py”,第 1666 行,在图中 行 = [*self._get_lines(*args, data=data, **kwargs)]

调用中的文件“C:\Anaconda\lib\site-packages\matplotlib\axes_base.py”,第 225 行 来自 self._plot_args(this, kwargs)

文件“C:\Anaconda\lib\site-packages\matplotlib\axes_base.py”,第 366 行,在 _plot_args 线型、标记、颜色 = _process_plot_format(tup[-1])

文件“C:\Anaconda\lib\site-packages\matplotlib\axes_base.py”,第 106 行,_process_plot_format '格式字符串中无法识别的字符 %c' % c)

ValueError: 格式字符串中无法识别的字符 O

【问题讨论】:

  • 请务必发布完整的错误消息,并附上完整的追溯

标签: python matplotlib plot


【解决方案1】:

看起来file_col_copy 是一个字符串。函数plt.plot() 需要许多不同格式的参数,但通常,您希望传递plt.plot(x, y),其中xy 都是数组,而不是字符串。

【讨论】:

  • 它有帮助。 file_col 是个问题,因为我在创建数组时没有返回它。
猜你喜欢
  • 2022-12-01
  • 1970-01-01
  • 2022-12-02
  • 2023-03-18
  • 1970-01-01
  • 2022-11-19
  • 2017-01-06
  • 2017-09-07
  • 1970-01-01
相关资源
最近更新 更多