2012-05-15 40 views
-1

我正在從使用ASCII字符254作爲分隔符的數據庫中提取數據。我難以理解如何搜索爲254返回的字符串,然後根據它創建字典?如何在Jython中搜索ASCII字符的字符串?

我的Python類

import sys 
sys.path.append('C:\\IBM\\UniDK\\uojsdk\\lib\\asjava.jar') 
import os.path 

from asjava.uniclientlibs import UniDynArray, UniDataSet 
from asjava.uniobjects import UniSession, UniFile 
from asjava.uniobjects import UniSubroutineException 
from asjava.uniobjects.UniObjectsTokens import AT_FM 

class u2py : 

     def __init__ (self): 
      self.conn = UniSession() 

     def get_db(self): 
      """ establish a connection to Unidata """ 
      username = 'dbuser' 
      password = 'SuperSecretPassword' 
      return self.conn.connect("host", username, password, "ACCOUNT") 

     def close_db(self): 
      """ drop connection to Unidata """ 
      if self.conn.isActive(): 
       self.conn.disconnect() 

     def open_file(self,file_name,rec): 
      """ open a U2 file read a record and return it """ 
      uvfile = self.conn.open(file_name) 
      uvrec = uvfile.read(rec) 
      dataset = uvrec 
      uvfile.close() 
      return dataset 

     def open_field(self,file_name,rec,field): 
        """ open a U2 file read a record and return it """ 
        uvfile = self.conn.open(file_name) 
        uvrec = uvfile.readField(rec,field) 
        dataset = uvrec 
        uvfile.close() 
        return dataset 

這是我在控制檯代碼:

from u2py import * 

u2 = u2py() 
c = u2.get_db() #creates the connection to the DB 
C#actually makes the connection 
rec = u2.open_file('SOFILE','SO133700') 
print rec 

,然後打印此屏幕:

ZZZAA■XN3■CEL■931819501■20020215■BWI/PP■■ 

「■」 其實是爲字段標記字符(254)

編輯:

當我使用這個:

>>>rec = u2.open_file('SOFILE','SO133699').split(chr(254)) 

我得到這個錯誤

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
AttributeError: 'asjava.uniclientlibs.UniString' object has no attribute 'split' 

編輯和最後的答案:

使用UniObjects的Java

rec.toString().split(chr(254)) 

成功!!!!

+2

[你有什麼嘗試?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) –

+0

你到目前爲止嘗試過什麼?你看過'split()'..這會生成一個列表。不知道你想用什麼作爲鍵/值對。 – Levon

+3

有沒有ASCII字符254. – geoffspear

回答

5

your_string.split(chr(254)),例如,

>>> "foo\xFEbar\xFEbaz".split(chr(254)) 
['foo', 'bar', 'baz'] 

這將返回一個列表。如何從這裏建立一本字典我會留給你,因爲我不知道你想要什麼鍵和值。

+0

謝謝。這是我正在尋找的。我想我可以遍歷基於254值標記的字符串,並根據返回的值標記的總數構建密鑰。 – Norm

+0

現在我有另一個問題,但我認爲這是一個與我正在使用的Java庫有關的問題。 – Norm

0

要搜索的ASCII字符254串,你可以這樣做:

if '\xFE' in db_string: 
    # 254 char was in the string 

,或者找到的

delimiter_pos = db_string.find('\xFE') # will be -1 if delimiter not found 

你得的位置來解釋你的意思是什麼但是「更詳細地創建基於該字典的字典」。你的意思是列名和值的字典嗎?數據庫中的響應字符串的示例是什麼?