当我尝试从python的CSV文件获取图形时出现Typeerror错误 [英] Typeerror occours when I tried to get a graphic from CSV file in python

查看:75
本文介绍了当我尝试从python的CSV文件获取图形时出现Typeerror错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import os
from matplotlib import pyplot as pyplot
from collections import defaultdict
import csv
import numpy as np
path = r'C:\Users\AK6PRAKT\Desktop\6daten'
dirs = os.listdir(path) 
s = []
x = []
y = []
names = []
fig = pyplot.figure()
for i in dirs:                             
    if os.path.splitext(i)[1] == ".csv": 
        f = open(path+"/"+i)
        iter_f = iter(f);
        str = ""
        for line in iter_f: 
              str = str + line
        s.append(str) 
        with open(path+"/"+i,'r') as r:
            lines=r.readlines()
        with open(path+"/"+i,'w') as w:
            for row in lines:
                if 'Date' not in row:
                    w.write(row)
        columns = defaultdict(list)
        with open(path+"/"+i) as f:
            reader = csv.reader(f)
            for row in reader:
                for (i,v) in enumerate(row):
                    columns[i].append(v)
                    list_temp1 = columns[0]
                    list_temp1 = np.array(list_temp1)
                    list_temp2 = columns[2]
                    list_temp2 = np.array(list_temp2)
            print(list_temp1,list_temp2)
            y.append(float(list_temp2))                                                    
            names.append(list_temp1)
x = range(len(names))
pyplot.ylim((0, 40)) 
my_y_ticks = np.arange(0, 40, 10)
pyplot.plot(x,y, linewidth=2)
pyplot.xticks(x,names,rotation = 90)
fig = pyplot.figure(figsize=(10,10))
pyplot.show()

如果只有几个csv文件,此代码将非常有效.但是我想在实践中应用此代码,我需要分析超过200000的csv文件并制作图形,以便我可以明显地看到这些数据的波动,但是当我要导入这些宏伟的文件时,会出现以下错误:

This code works very good if there are only several csv files. BUT i want to apply this code in practice, i need to analyse more than 200000 csv file and make a graphic so that I can see the fluctuation of these datas obviously, but when i want to import these magnificent files, the following Error occours:

File "C:/Users/AK6PRAKT/headerremover.py", line 44, in <module>

y.append(float(list_temp2))

TypeError: only size-1 arrays can be converted to Python scalars

我真的不知道如何解决这个问题,如果有人可以帮助我,非常感谢!!!!!!

I really have no idea about solving that, if anyone can help me, thanks a lot!!!!!!

推荐答案

此错误代码是由于尝试将长度大于1的numpy数组强制转换为浮点数导致的:

This error code results from an attempt to cast a numpy array to float, which has length > 1:

示例:

float(np.array([3,2,1]))
Traceback (most recent call last):

  File "<ipython-input-22-e396216bb2ea>", line 1, in <module>
    float(np.array([3,2,1]))

TypeError: only length-1 arrays can be converted to Python scalars

但是:

float(np.array([3]))
: 3.0

因此此错误在您的行中发生(引用错误消息时为44)

So this error occurs in your line (44, to cite the error message)

y.append(float(list_temp2))

如果list_temp2具有多个条目,则

.因此,它应该与处理的文件数量无关.

in case of list_temp2 has more than one entry. So it should have nothing to do with how many files you process.

但是:

就像已经提到的乔治一样-假设您的代码比任务所需的要复杂一些.
这里只是我的一些想法...

Like Georgy already mentioned - let's say your code is a little more complicated than the task requires.
Here just a few thoughts of mine...

  1. 如果您需要目录中所有csv -files的列表,您是否看过python的glob -module?它也能够搜索子目录...
  2. 您遍历了一个文件以进行读取,再次进行了写操作,但没有包含日期"的行,然后又再次读取了该文件...?太多了,IIUC.您不能只使用最后一个循环并将list_temp-stuff放在有条件的条件下,以测试是否包含日期"吗?
  3. 为什么您fig = pyplot.figure(figsize=(10,10))紧接plt.show()末尾?这将导致未使用的空白图形.
  4. headerremover.py与循环和if 'Date' not in row结合使用,看起来文件中是否还有很多标题行(也在中间)-是这样吗?因为如果只在每个文件的顶部具有n行的标头部分,那么将会有更简单的方法来跳过这些行...
  1. if you need a list of all csv-files in a directory, did you have alook at python's glob-module? It's capable of searching subdirectories, too...
  2. You iterate over a file for reading, again for writing without lines which contain "Date" and then again you read it it again...? That's too much, IIUC. Couldn't you just use the last loop and put your list_temp-stuff there in a conditional, which tests for "contains 'Date'"?
  3. Why do you fig = pyplot.figure(figsize=(10,10)) right before plt.show() at the end? This should lead to an unused empty figure.
  4. headerremover.py combined with your loop and if 'Date' not in row looks like if there are many header lines, also in the middle, in your files - is that true? Because if you just would have a header section of n lines at the top of each file, there would be again much simpler ways to skip these lines...

这篇关于当我尝试从python的CSV文件获取图形时出现Typeerror错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆