how to import csv data and use the imported data

43 views
Skip to first unread message

wangfen...@gmail.com

unread,
Sep 18, 2017, 10:31:52 AM9/18/17
to web2py-users
I am phD students in Hiroshima University, Japan. I try to use web2py to establish an app for my research about environment management of the Seto Inland Sea,Japan. In the app, users upload their csv data, and will get the results
here is my controller file and view file
#controller data.py
def data(): 
    import numpy as np
    import pandas as pd
    dat = pd.read_csv('C:\Users\XX\Desktop\TEST.csv')
    sal = dat['salinity']
    N2 = dat['N2']
    chl = dat['chla']
    return dict(sal=sal, N2=N2, chl=chl)
#data/data html
{{extend 'layout.html'}}
#show the raw data
<h1>这是 This is raw data</h1>
<h5>sal={{=sal}}</h5>
<h5>N2={{=N2}}</h5>
<h5>chl={{=chl}}</h5>
{{import numpy as np}}
{{from scipy.optimize import leastsq}}
#calculation
#data standardization
{{s_sal=((chl-np.mean(sal))/np.std(sal))}}
{{s_N2=((chl-np.mean(N2))/np.std(N2))}}

#model fitting by leastsq 
{{def func1(t,x,y):}}
    {{return 1/(t[0]+t[1]*np.exp(t[2]*x))-y}}
{{t0=np.array([1.0,1.0,1.0])}}

{{for i in np.arange(0,2.0,0.05):}}
    {{index = i*s_sal + (2-i)*s_N2}}
    {{cf = leastsq(func1,t0,args=(index,chl))}}

#show the results
<li>
"Model: "{{=i}}"*salinity+"{{=2-i}}"*N2" &nbsp;&nbsp; "Equation: 1/("{{=round(cf[0][0],2)}}"+"{{=round(cf[0][1],2)}}"*EXP("{{=round(cf[0][2],2)}}"*x))"
</li>
{{pass}}

I am fresh in coding work and web development, so some easy problems in this field is difficult for me. 
In this case what should I add in Model part (database) and how to get the csv content from the corresponding database.
Thanks all. 

黄祥

unread,
Sep 18, 2017, 5:52:31 PM9/18/17
to web2py-users
web2py has dal function for import csv (.import_from_csv_file() )
ref:

i think you should define the table that meet your data in the model first, then use dal function import_from_csv_file()

best regards,
stifan
Reply all
Reply to author
Forward
0 new messages