I tried importing float numbers from P-I curve.txt file which contains my data. however i get an error when converting this into float. i used the following code.
with open('C:/Users/Kevin/Documents/4e Jaar/fotonica/Metingen/P-I curve.txt') as csvfile:
data= csv.reader(csvfile, delimiter = '\t')
current=[]
P_15=[]
P_20=[]
P_25=[]
P_30=[]
P_35=[]
P_40=[]
P_45=[]
P_50=[]
for row in data:
current.append(float(row[0].replace(',','.')))
P_15.append(float(row[2].replace(',','.')))
P_20.append(float(row[4].replace(',','.')))
P_25.append(float(row[6].replace(',','.')))
P_30.append(float(row[8].replace(',','.')))
P_35.append(float(row[10].replace(',','.')))
P_40.append(float(row[12].replace(',','.')))
P_45.append(float(row[14].replace(',','.')))
P_50.append(float(row[16].replace(',','.')))
with this code i got the following error which i understand that row 2 is a string but if so then why did this error not occur for row 1. Is there any other data to import float numbers without using csv import? I have copied and pasted the data from excel to a .txt file.
returned error:
File "C:/Users/Kevin/Documents/Python Scripts/P-I curves.py", line 29, in <module>
P_15.append(float(row[2].replace(',','.')))
ValueError: could not convert string to float:
I tried another following code:
import pandas as pd
df=pd.read_csv('C:/Users/Kevin/Documents/4e Jaar/fotonica/Metingen/P-I curve.txt', decimal=',', sep='\t',header=0,names=['current','15','20','25','30','35','40','45','50'] )
#curre=df['current']
print(current)
The txt file has a header and looks like this:
1.8 1.9 0.4 1.9 0.4 1.9 0.4 1.9 0.4
3.8 1.9 1.3 1.9 1.3 1.9 1.3 1.9 1.2
5.8 2.0 2.5 2.0 2.4 2.0 2.3 2.0 2.2
7.8 2.0 3.7 2.0 3.6 2.0 3.5 2.0 3.4
9.8 2.1 5.2 2.0 5.1 2.0 4.9 2.0 4.7
11.8 2.1 6.9 2.1 6.7 2.1 6.4 2.1 6.1
13.8 2.1 9.0 2.0 8.6 2.1 8.2 2.1 7.8
15.8 2.1 11.5 2.1 10.8 2.1 10.2 2.1 9.7
17.8 2.2 14.7 2.2 13.7 2.2 12.7 2.2 11.8
19.8 2.2 19.5 2.2 17.5 2.2 15.9 2.2 14.5
21.8 2.2 28.9 2.2 23.6 2.2 20.3 2.2 17.9
23.8 2.3 125.8 2.2 38.4 2.2 27.8 2.2 22.8
25.8 2.3 1669.0 2.3 634.0 2.3 51.7 2.3 31.4
27.8 2.3 3142.0 2.3 2154.0 2.3 982.0 2.3 62.2
29.8 2.3 4560.0 2.3 3594.0 2.3 2460.0 2.3 1075.0
31.8 2.3 5950.0 2.3 5010.0 2.3 3872.0 2.3 2540.0
33.8 2.4 7320.0 2.4 6360.0 2.4 5230.0 2.3 3880.0
35.8 2.4 8670.0 2.4 7700.0 2.4 6550.0 2.4 5210.0
37.8 NaN NaN NaN NaN 2.4 7850.0 2.4 6480.0
39.8 NaN NaN NaN NaN NaN NaN NaN NaN
41.8 NaN NaN NaN NaN NaN NaN NaN NaN
Name: current, dtype: float64
python seems to be returning everything instead of just line 1 which i want by printing the header current. I only want to take this line so i can save it as in an array. But How do i specifically draw the line with header current out of the data?.
I am not sure why it returned everything but i think that there is something wrong with encoding because i copied and pasted the data from excel.
Please look at the image of how the .txt looks like when copied from excel.
i tried out another short code (i also deleted the header manually for the .txt file!!), see description below:
data=np.loadtxt('C:/Users/Kevin/Documents/4e Jaar/fotonica/Metingen/ttest.txt',delimiter='\t')
data=float(data.replace(',','.'))
print(data[0])
with this code, i get the followin error.
ValueError: could not convert string to float: b'1,8'
I find this weird to occur. Is floating and replacing not enough for this

import pandas as pd; df=pd.read_csv(filename, decimal=',', sep='\t')