導航:首頁 > 編程語言 > nls函數python

nls函數python

發布時間:2022-08-18 18:53:31

A. 我用python連接資料庫查詢,字元是外文字元,獲取後都是「」

編寫的python腳本中需要加入如下幾句:
import os
os.environ['NLS_LANG']
= 'SIMPLIFIED CHINESE_CHINA.UTF8'
這樣可以保證select出來的中文顯示沒有問題。

B. python怎麼設置環境變數

os.environ
例如,設置 oracle 編碼
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'

C. python調用oracle的sql中wm_concat問題:

利用python調用sqlplus來輸出結果的例子:
import os
import sys
from subprocess import Popen, PIPE
sql = """
set linesize 400
col owner for a10
col object_name for a30
select owner, object_name
from dba_objects
where rownum<=10;
"""
proc = Popen(["sqlplus", "-S", "/", "as", "sysdba"], stdout=PIPE, stdin=PIPE, stderr=PIPE)
proc.stdin.write(sql)
(out, err) = proc.communicate()
if proc.returncode != 0:
print err
sys.exit(proc.returncode)
else:
print out
用Python查詢Oracle,當然最好用cx_Oracle庫,但有時候受到種種限制,不能安裝Python第三方庫,就得利用現有資源,硬著頭皮上了。
用Python調用SqlPlus查詢Oracle,首先要知道SqlPlus返回結果是什麼樣的:
(這是空行)
Number Name Address
------------ ----------- ------------------
1001 張三 南京路
1002 李四 上海路
第1行是空行,第2行是欄位名稱,第3行都是橫杠,有空格隔開,第4行開始是查詢到的結果。
在查詢結果規整的情況下,根據第3行可以很清晰的看到結構,用Python解析起來也比較方便。但是,如果一張表欄位特別多,記錄數也相當多,那麼默認情況下調用SqlPlus查詢出的結果會比較亂,這就需要在調用查詢之前做一些設定,比如:
set linesize 32767
set pagesize 9999
set term off verify off feedback off tab off
set numwidth 40
這樣的調用查詢結果就比較規整了。接下來就是用強大的Python來解析查詢結果。
這里封裝了一個函數,可以根據傳入的SQL語句查詢並解析結果,將每行結果存到列表中,列表中的每個元素是一個欄位名稱與值的映射。
#!/usr/bin/python
#coding=UTF-8
'''
@author: 雙子座@開源中國
@summary: 通過SqlPlus查詢Oracles資料庫
'''
import os;
os.environ['NLS_LANG'] = 'AMERICAN_AMERICA.AL32UTF8'
gStrConnection = 'username/[email protected]:1521/ora11g'
#解析SqlPlus的查詢結果,返回列表
def parseQueryResult(listQueryResult):
listResult = []
#如果少於4行,說明查詢結果為空
if len(listQueryResult) < 4:
return listResult
#第0行是空行,第1行可以獲取欄位名稱,第2行可獲取SQLPlus原始結果中每列寬度,第3行開始是真正輸出
# 1 解析第2行,取得每列寬度,放在列表中
listStrTmp = listQueryResult[2].split(' ')
listIntWidth = []
for oneStr in listStrTmp:
listIntWidth.append(len(oneStr))
# 2 解析第1行,取得欄位名稱放在列表中
listStrFieldName = []
iLastIndex = 0
lineFieldNames = listQueryResult[1]
for iWidth in listIntWidth:
#截取[iLastIndex, iLastIndex+iWidth)之間的字元串
strFieldName = lineFieldNames[iLastIndex:iLastIndex + iWidth]
strFieldName = strFieldName.strip() #去除兩端空白符
listStrFieldName.append(strFieldName)
iLastIndex = iLastIndex + iWidth + 1
# 3 第3行開始,解析結果,並建立映射,存儲到列表中
for i in range(3, len(listQueryResult)):
oneLiseResult = unicode(listQueryResult[i], 'UTF-8')
fieldMap = {}
iLastIndex = 0
for j in range(len(listIntWidth)):
strFieldValue = oneLiseResult[iLastIndex:iLastIndex + listIntWidth[j]]
strFieldValue = strFieldValue.strip()
fieldMap[listStrFieldName[j]] = strFieldValue
iLastIndex = iLastIndex + listIntWidth[j] + 1
listResult.append(fieldMap)
return listResult
def QueryBySqlPlus(sqlCommand):
global gStrConnection
#構造查詢命令
strCommand = 'sqlplus -S %s <<!\n' % gStrConnection
strCommand = strCommand + 'set linesize 32767\n'
strCommand = strCommand + 'set pagesize 9999\n'
strCommand = strCommand + 'set term off verify off feedback off tab off \n'
strCommand = strCommand + 'set numwidth 40\n'
strCommand = strCommand + sqlCommand + '\n'
#調用系統命令收集結果
result = os.popen(strCommand)
list = []
for line in result:
list.append(line)
return parseQueryResult(list)
其中os.environ['NLS_LANG']的值來自
select userenv['language'] from al;
在調用的時候,只要類似:
listResult = QueryBySqlPlus('select * from studentinfo')
然後就可以用循環列印出結果了。

D. R語言使用nls擬合,為什麼總說循環次數大於50

nls的數據源必須有誤差。不能精確等於公式返回值(零殘差)。循環次數大於50通常是使用 函數精確返回值 作為數據源去擬合函數。必須給y值加上隨機誤差。


z=function(x,a,b){a*sin(x)+b*cos(x)}
x=seq(1,10,9/500)
y=z(x,1,1)#a=1b=1是期望擬合出的結果。
cor=data.frame(x=x,y=y)
cor$res=runif(length(cor$x),min=-0.005,max=0.005)
cor$yres=cor$y+cor$res
#yres=y加上隨機誤差,y是精確返回值
>nls(cor$yres~z(cor$x,a,b),data=cor,start=list(a=0.8,b=1.3))
Nonlinearregressionmodel
model:cor$yres~z(cor$x,a,b)
data:cor
ab
0.99991.0002
resialsum-of-squares:0.004213

:1
Achievedconvergencetolerance:2.554e-07

#使用精確返回值擬合就會出錯。
>nls(cor$y~z(cor$x,a,b),data=cor,start=list(a=1,b=1))
Errorinnls(cor$y~z(cor$x,a,b),data=cor,start=list(a=1,b=1)):
循環次數超過了50這個最大值

E. oracle中的NLS_INITCAP、NLS_LOWER、NLS_UPPER函數

SCHINESE_RADICAL_M 按照部首(第一順序)、筆劃(第二順序)排序

SCHINESE_STROKE_M 按照筆劃(第一順序)、部首(第二順序)排序

SCHINESE_PINYIN_M 按照拼音排序
排序用
比如select nls_initcap('網路知道', 'nls_sort= SCHINESE_STROKE_M')
from al;

閱讀全文

與nls函數python相關的資料

熱點內容
同性戀激情電影 瀏覽:435
手機怎麼訪問自己搭建的伺服器地址 瀏覽:361
運演算法則題庫 瀏覽:989
做程序員最開心的方式 瀏覽:748
蘋果手機圖片無法存入文件夾 瀏覽:328
張浩給豬治不孕的電影名字 瀏覽:51
乙巳日演算法精論 瀏覽:690
程序員戀愛觀視頻 瀏覽:807
CK免費電影官網 瀏覽:80
程序員相親打包剩菜 瀏覽:339
秘制pdf 瀏覽:738
成都金稅移動網路伺服器地址 瀏覽:655
程序員進企業要考試嗎 瀏覽:689
萍鄉數控編程培訓怎麼樣 瀏覽:535
類似保羅和媽媽的電影 瀏覽:420
luapython哪個值得學習 瀏覽:96
cad2007連續標注命令 瀏覽:651
通路雲伺服器還可以做什麼用 瀏覽:191
完美男人法國版講的是什麼 瀏覽:937
大露的電影 瀏覽:149