-1

我正在从使用 ASCII 字符 254 作为分隔符的数据库中提取数据。我很困惑如何搜索为 254 返回的字符串,然后根据它创建字典?

我的蟒蛇课

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■■

“■”实际上是字段标记 chr(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)) 

成功!!!!

4

2 回答 2

5

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

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

这将返回一个列表。如何从中构建字典我将留给你,因为我不知道你想要什么键和值。

于 2012-05-15T15:39:17.540 回答
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

不过,您必须更详细地解释“基于此创建字典”的含义。你的意思是列名和值的字典吗?来自数据库的响应字符串的示例是什么?

于 2012-05-15T15:40:51.097 回答