Python 从 csv 文件插入
Python interpolate from csv file
我正在尝试从 csv 文件中插入数据。前两列大小相同。我之前在 txt 文件中使用相同的数据取得了成功,这是我第一次尝试使用 csv 文件。任何帮助,将不胜感激。
示例 csv 文件
wave,transmittance
406.06,0.384
420.06,0.398
440.85,0.416
....,.....
import numpy as np
import csv
from scipy import interpolate
# column 0 wavelength
# column 1 transmittance
# .....
with open('CCD.csv','r') as csv_file:
next(csv_file)
data = list(csv.reader(csv_file))
x=[column[0] for column in data]
y=[column[1] for column in data]
I = interpolate.InterpolatedUnivariateSpline(x, y)
print(x)
Output:
UFuncTypeError
<ipython-input-9-fa767336938e> in <module>
12 y=[column[1] for column in data]
13
---> 14 I = interpolate.InterpolatedUnivariateSpline(x, y)
UFuncTypeError: ufunc 'subtract' did not contain a loop with signature matching types (dtype('<U11'), dtype('<U11')) -> dtype('<U11')
已编辑:
with open('CCD.csv','r') as csv_file:
reader = csv.reader(csv_file)
next(csv_file) # discard header
data = list(reader)
x1=[column[0] for column in data]
y1=[column[1] for column in data]
I2 = interpolate.InterpolatedUnivariateSpline(x1, y1)
z2 = I2(550)
print(z2)
UFuncTypeError Traceback (most recent call last)
<ipython-input-4-a8127aad6fce> in <module>
6 y1=[column[1] for column in data]
7
----> 8 I2 = interpolate.InterpolatedUnivariateSpline(x1, y1)
9 z2 = I2(550)
10 print(z2)
UFuncTypeError: ufunc 'subtract' did not contain a loop with signature matching types (dtype('<U11'), dtype('<U11')) -> dtype('<U11')
你很接近。您需要在首次打开文件时创建 reader 对象, 然后 使用 reader 迭代您的行:
import csv
data = None
with open('CCD.csv', newline='') as csv_file:
reader = csv.reader(csv_file)
next(csv_file) # discard header
data = list(reader)
x1 = [float(row[0]) for row in data]
y1 = [float(row[1]) for row in data]
print(x1)
print(y1)
我得到:
[406.06, 420.06, 440.85]
[0.384, 0.398, 0.416]
不过,我明白你是如何做到的,只是来自逐行迭代文本文件。很好的尝试! :)
此外,您不需要指定读取模式'r'
,这是默认设置;但对于 CSV 文件,建议在大多数情况下,您希望 newline=''
以便 Python 保留所有换行符(NL、LF、CRLF),就像 CSV reader 处理的那样,根据 CSV 规范。
或者,您可以在阅读 CSV 时构建 x
和 y
:
x, y = [], []
with open('CCD.csv', newline='') as csv_file:
reader = csv.reader(csv_file)
next(csv_file) # discard header
for row in reader:
x.append(float(row[0]))
y.append(float(row[1]))
print(x)
print(y)
与上面的打印结果相同。
这就是我使用 pandas 进行管理的方式,但我仍然对没有 pandas
的替代方法感到好奇
import pandas as pd
df = pd.read_csv('CCD.csv')
x = df['KAF16200wave'].to_numpy()
y = df['KAF16200transmitence'].to_numpy()
I = interpolate.InterpolatedUnivariateSpline(x, y)
z = I(550)
print("QE:",z)
QE: 0.589681398010309
我正在尝试从 csv 文件中插入数据。前两列大小相同。我之前在 txt 文件中使用相同的数据取得了成功,这是我第一次尝试使用 csv 文件。任何帮助,将不胜感激。 示例 csv 文件
wave,transmittance
406.06,0.384
420.06,0.398
440.85,0.416
....,.....
import numpy as np
import csv
from scipy import interpolate
# column 0 wavelength
# column 1 transmittance
# .....
with open('CCD.csv','r') as csv_file:
next(csv_file)
data = list(csv.reader(csv_file))
x=[column[0] for column in data]
y=[column[1] for column in data]
I = interpolate.InterpolatedUnivariateSpline(x, y)
print(x)
Output:
UFuncTypeError
<ipython-input-9-fa767336938e> in <module>
12 y=[column[1] for column in data]
13
---> 14 I = interpolate.InterpolatedUnivariateSpline(x, y)
UFuncTypeError: ufunc 'subtract' did not contain a loop with signature matching types (dtype('<U11'), dtype('<U11')) -> dtype('<U11')
已编辑:
with open('CCD.csv','r') as csv_file:
reader = csv.reader(csv_file)
next(csv_file) # discard header
data = list(reader)
x1=[column[0] for column in data]
y1=[column[1] for column in data]
I2 = interpolate.InterpolatedUnivariateSpline(x1, y1)
z2 = I2(550)
print(z2)
UFuncTypeError Traceback (most recent call last)
<ipython-input-4-a8127aad6fce> in <module>
6 y1=[column[1] for column in data]
7
----> 8 I2 = interpolate.InterpolatedUnivariateSpline(x1, y1)
9 z2 = I2(550)
10 print(z2)
UFuncTypeError: ufunc 'subtract' did not contain a loop with signature matching types (dtype('<U11'), dtype('<U11')) -> dtype('<U11')
你很接近。您需要在首次打开文件时创建 reader 对象, 然后 使用 reader 迭代您的行:
import csv
data = None
with open('CCD.csv', newline='') as csv_file:
reader = csv.reader(csv_file)
next(csv_file) # discard header
data = list(reader)
x1 = [float(row[0]) for row in data]
y1 = [float(row[1]) for row in data]
print(x1)
print(y1)
我得到:
[406.06, 420.06, 440.85]
[0.384, 0.398, 0.416]
不过,我明白你是如何做到的,只是来自逐行迭代文本文件。很好的尝试! :)
此外,您不需要指定读取模式'r'
,这是默认设置;但对于 CSV 文件,建议在大多数情况下,您希望 newline=''
以便 Python 保留所有换行符(NL、LF、CRLF),就像 CSV reader 处理的那样,根据 CSV 规范。
或者,您可以在阅读 CSV 时构建 x
和 y
:
x, y = [], []
with open('CCD.csv', newline='') as csv_file:
reader = csv.reader(csv_file)
next(csv_file) # discard header
for row in reader:
x.append(float(row[0]))
y.append(float(row[1]))
print(x)
print(y)
与上面的打印结果相同。
这就是我使用 pandas 进行管理的方式,但我仍然对没有 pandas
的替代方法感到好奇import pandas as pd
df = pd.read_csv('CCD.csv')
x = df['KAF16200wave'].to_numpy()
y = df['KAF16200transmitence'].to_numpy()
I = interpolate.InterpolatedUnivariateSpline(x, y)
z = I(550)
print("QE:",z)
QE: 0.589681398010309