weixin_49381033
Alone,Pt ll
2020-12-23 22:46
采纳率: 71.4%
浏览 128

为什么报错NameError: name 'GBK' is not defined?如何解决?

# coding=utf-8
import sys
a_utf8='人生苦短'
a_gbk=a_utf8.encode(GBK)
print ('a_gbk')
C:\Users\zbj15010857936\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/zbj15010857936/PycharmProjects/pythonProject/main.py
Traceback (most recent call last):
  File "C:/Users/zbj15010857936/PycharmProjects/pythonProject/main.py", line 4, in <module>
    a_gbk=a_utf8.encode(GBK)
NameError: name 'GBK' is not defined

Process finished with exit code 1
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • sinat_33263516
    在黎明的反思 2020-12-24 10:30
    已采纳

    GBK应该传入字符串类型的  'GBK'

    点赞 评论
  • weixin_43870646

    先编码再解码decode,encode的使用,查查资料

    点赞 评论
  • niu1024
    niu1024 2020-12-24 18:35

    GBK没有加引号,所以python编译器会把它识别成一个变量,它的名字叫GBK,而不是真正的GBK,但是加引号时,他就是一个字符串'GBK',所以加了引号python才不会报错。

    点赞 评论

相关推荐