Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用于连接(某物),但结果未连接_Python_Sql - Fatal编程技术网

Python 用于连接(某物),但结果未连接

Python 用于连接(某物),但结果未连接,python,sql,Python,Sql,我期待的结果是 >>> message_to bits('') '' >>> message_to_bits('hello') '0110100001100101011011000110110001101111' 但我得到的是空字符串和字符串的错误,第一个字符只有1和0: def char_to_bits(char): """char_to_bits(char) -> string Convert the input ASCII ch

我期待的结果是

>>> message_to bits('')
''
>>> message_to_bits('hello')
'0110100001100101011011000110110001101111'
但我得到的是空字符串和字符串的错误,第一个字符只有1和0:

def char_to_bits(char):
    """char_to_bits(char) -> string

    Convert the input ASCII character to an 8 bit string of 1s and 0s.

    >>> char_to_bits('A')
    '01000001'
    """
    result = ''
    char_num = ord(char)
    for index in range(8):
        result = get_bit(char_num, index) + result
    return result

def get_bit(int, position):
    """get_bit(int, position) -> bit

    Return the bit (as a character, '1' or '0') from a given position
    in a given integer (interpreted in base 2).

    The least significant bit is at position 0. The second-least significant
    bit is at position 1, and so forth.

    >>> for pos in range(8):
    ...     print(b.get_bit(167, pos))
    ...
    1
    1
    1
    0
    0
    1
    0
    1
    """
    if int & (1 << position):
        return '1'
    else:
        return '0'

def message_to_bits(message):
    for char in message:
        result="".join(str(bits.char_to_bits(char))) 
    return result
def char_到_位(char):
“”“char\u到_位(char)->字符串
将输入的ASCII字符转换为1和0的8位字符串。
>>>字符到字节('A')
'01000001'
"""
结果=“”
char_num=ord(char)
对于范围(8)中的索引:
结果=获取位(字符数,索引)+结果
返回结果
def get_位(整数,位置):
“”“获取位(int,position)->位
从给定位置返回位(作为字符“1”或“0”)
在给定整数中(以基数2解释)。
最低有效位位于位置0。第二个最低有效位
位位于位置1,依此类推。
>>>对于范围(8)内的位置:
…打印(b.get_位(167,位置))
...
1.
1.
1.
0
0
1.
0
1.
"""

如果int&(1要加入两次:

def message_to_bits(message):
  return "".join("".join(str(bits.char_to_bits(char))) for char in message)

第一个连接用于字符中的位,第二个连接用于消息中的字符。

但是空字符串呢???@hwanghyungchae:对于空字符串输入,消息中字符的
生成器应该生成一个空的iterable,所以结果应该是空字符串,对吗?对不起,我现在正在使用平板电脑,无法访问验证结果。你不必道歉,伙计。我现在知道了,我把它放在下面:如果len(message)==0:返回“”@hwanghyungchae:我以交互方式验证了这应该可以工作,而无需显式检查:
“”。join('123'表示“”中的字符)
返回
'
。如果您想进行显式检查,我只需检查
if message:
而不是
if len(message)==0
——第一种形式更像蟒蛇。最后,如果你觉得这个答案有帮助,请随意投票和/或接受。我没有足够的声誉投票。无论如何,非常感谢!