简体   繁体   English

Python3x中的字符串与Python2.x中的字符串不同,但仍能正常工作

[英]string is not same in Python3x as for Python2.x but Still it is working

I am confused with the strings in Python, 我对Python中的字符串感到困惑,

Consider this code, 考虑一下这段代码,

fo = open("test.txt", "wb")
fo.write(bytes('Sufiyan','UTF-8'))

If I were to run this code, It would give this error, 如果我要运行此代码,则会出现此错误,

 Traceback (most recent call last): File "C:\\Users\\Sufiyan\\Desktop\\AES\\a.py", line 2, in <module> fo.write('Sufiyan') TypeError: 'str' does not support the buffer interface 

Therefore, I have to convert it into bytes and provide the encoding type. 因此,我必须将其转换为bytes并提供编码类型。

fo = open("test.txt", "wb")
fo.write(bytes('Sufiyan','UTF-8'))

This works, because If you use Python3x then string is not the same type as for Python 2.x, you must cast it to bytes (encode it). 这行得通,因为如果您使用Python3x,那么string与Python 2.x的字符串类型不同,则必须将其强制转换为字节(对其进行编码)。

Now, When I am using this code to write to a file, 现在,当我使用此代码写入文件时,

def BB_cf(file, *args, **kwargs): #Create files

    try:

        fo = open(file)
        fo.close()

    except IOError:

        print (file, 'not Found')
        print ("Creating file.....")
        fo = open(file, "w")
        print (file,"Created Successfully!")

        if file == 'input_Data.txt':
            print(file,"is Empty..\nWriting Data..")
            text = kwargs.get('text', None)
            fo.write(text)
            print("'"+text+"'", "is written in",file)
            fo.close()

        fo.close()

.

BB_cf('input_Data.txt', text='Sufiyan Ghori')

As you can see in the 4th last line, fo.write(text) , I haven't encoded it, but the code is still working. 如您在最后第四行fo.write(text) ,我尚未对其进行编码,但是代码仍在工作。

Why the code is working without encoding it ? 为什么代码没有编码就可以工作?

Now if I cast it to bytes , It will give the following error, 现在,如果我将其转换为bytes ,它将给出以下错误,

Traceback (most recent call last): 追溯(最近一次通话):

File "C:\\Users\\Sufiyan\\Desktop\\AES\\BlackBox.py", line 47, in 文件“ C:\\ Users \\ Sufiyan \\ Desktop \\ AES \\ BlackBox.py”,第47行,在

 BB_cf('input_Data.txt', text='Sufiyan Ghori') File 

"C:\\Users\\Sufiyan\\Desktop\\AES\\BlackBox.py", line 41, in BB_cf BB_cf中的“ C:\\ Users \\ Sufiyan \\ Desktop \\ AES \\ BlackBox.py”,第41行

 fo.write(bytes(text,'UTF-8')) TypeError: must be str, not bytes 

Both the above codes are running using Python3x, The first one wants me to encode the string into Bytes while the 2nd one is running without encoding. 上面的两个代码都使用Python3x运行,第一个要我将string编码为Bytes而第二个要运行而无需编码。

The first time around, you open your file by doing fo = open("test.txt", "wb") . 第一次,您通过执行fo = open("test.txt", "wb")打开文件。 The second time, you do fo = open(file, "w") . 第二次,您执行fo = open(file, "w") The string "wb" indicates to Python that you want to write to the file using only bytes, instead of strings, whereas the string "w" in your second example states the reverse. 字符串"wb"向Python表示您只想使用字节而不是字符串来写入文件,而第二个示例中的字符串"w"则相反。

Specifically, the documentation for the open function states: 具体来说, 开放功能的文档指出:

As mentioned in the Overview, Python distinguishes between binary and text I/O . 如概述中所述, Python区分二进制I / O和文本I / O。 Files opened in binary mode (including 'b' in the mode argument) return contents as bytes objects without any decoding. 以二进制模式打开的文件(包括mode参数中的'b') 以字节对象的形式返回内容,而无需进行任何解码。 In text mode (the default, or when 't' is included in the mode argument), the contents of the file are returned as str , the bytes having been first decoded using a platform-dependent encoding or using the specified encoding if given. 在文本模式下(默认设置,或者在mode参数中包含't'时), 文件内容以str形式返回 ,首先使用平台相关的编码或指定的编码(如果给定的话)对字节进行解码。

(emphasis added by me) (我加强调)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM