Error in processing read csv

Asked 2 years ago, Updated 2 years ago, 40 views

I would like to read the data in the csv file and display the maximum value in it, but the error is as follows:
(The csv file was measured by an oscilloscope.)
I look forward to hearing from someone.

program

import csv
import matplotlib.pyplot asplt
import numpy as np
csvfile=r'C:\Users\ryoma\Documents\python\test1khz.csv' for learning

f=open(csvfile, "r")
reader=csv.reader(f)
data = [v for v in reader ]
f.close()

np.max(data)

error message

TypeError Traceback (most recent call last)
<ipython-input-11-b6a52 add 7c26>in<module>
---->1np.max(data)

~\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py in amax(a,axis,out,keepdims,initial)
   2503     """
   2504 return_wrapreduction(a,np.maximum,'max',axis,None,out,keepdims=keepdims,
->2505 initial = initial)
   2506 
   2507 

~\Anaconda3\lib\site-packages\numpy\core\fromnumeric.py in_wrapreduction(obj,ufunc,method,axis,dtype,out,**kwargs)
     84 return reduction (axis=axis, out=out, **passkwargs)
     85 
--- >86 return ufunc.reduce(obj,axis,dtype,out,**passkwargs)
     87 
     88 

TypeError: cannot perform redundancy with flexible type

python python3

2022-09-30 18:02

1 Answers

The reason for the error is that the end element of the variable data list is a string. Since np.sum is basically the sum of the numbers, the sum of the strings is undefined and appears to be an error.If all the elements in the data variable list are numeric strings, you can convert numeric strings into numeric ones as follows:This should prevent the error from occurring in np.sum.

(Before Modification)

 data=[v for v in reader]

(Corrected)

 data=np.asarray ([v for v in reader], dtype=np.float32)


2022-09-30 18:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.