在Python3python中如何输出中文文

404 Not Found
The requested URL /q/1572/a-2421 was not found on this server.23:12 提问
Python的print输出中文的问题!!!!
求大神帮忙!!!
#/bin/usr/python
#coding:utf-8
print("输出:",num)
输出结果:
('\xe9\x9a\x8f\xe4\xbe\xbf\xef\xbc\x9a', 100)
为什么???但是如果只是输出中文的话是正常的!!!
按赞数排序
你用的是Python2.x 把括号去掉
print "输出:",num
Python的print函数输出中文字符串对齐问题的解决方案。......答案就在这里:----------------------Hi,地球人,我是问答机器人小S,上面的内容就是我狂拽酷炫叼炸天的答案,除了赞同,你还有别的选择吗?
# encoding: utf-8
print '哈哈哈哈哈'
#!/usr/bin/python
#encoding: utf-8
print('输入:',num)
print("输入:")
输出结果:
('\xe8\xbe\x93\xe5\x85\xa5\xef\xbc\x9a', 100)
print('输出数字:%d'%num)
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐python3 写入文件 中文乱码 - Shylock的个人空间 - 51Testing软件测试网 51Testing软件测试网-中国软件测试人的精神家园
python3 写入文件 中文乱码
& 11:25:07
/ 个人分类:
代码为:defsendtext(self):LOG.info("send text")text =self.edittext.toPlainText()# text = "你好"js_text="var InputMethod = require(\"yunos/system/InputMethod\").getInstance();InputMethod.commitString(\"%s\");process.exit(0);"%textprint(type(js_text))js_text=js_text.encode("utf8")withopen("./res/js/sendtext.js","wb")asf:f.write(js_text)self.__A.push("./res/js/sendtext.js","/data/")self.__A.shell2("node --harmony --output-console /data/sendtext.js")在使用pyqt的时候,获取一个Qedittext的值。是中文。带入后,js_text print的时候就是中文,但是,写入的时候,怎么样都是乱码。如果不给js_text转码的话,提示TypeError: 'str' does not support the buffer interface首先要给js_text转码然后再打开文件的时候是wb然后就可以很完美的显示中文。下次自動登錄
現在的位置:
& 綜合 & 正文
轉 Python3中文教程
Python已經是3.1版本了,與時俱進更新教程.
本文適合有Java編程經驗的程序員快速熟悉Python
本文程序在windows xp+python3.1a1 測試通過.
本文提到的idle指python shell,即安裝python後你在菜單看到的IDLE(python gui)
在idle里ctrl+n可以打開一個新窗口,輸入源碼後ctrl+s可以保存,f5運行程序.
凡打開新窗口即指ctrl+n的操作.
#打開新窗口,輸入:#! /usr/bin/python
# -*- coding: utf8 -*- s1=input("Input your name:")print("你好,%s" % s1)
* input("某字符串")函數:顯示"某字符串",並等待用戶輸入.
* print()函數:如何打印.
* 如何應用中文
* 如何用多行注釋'''
2 字符串和數字
但有趣的是,在javascript里我們會理想當然的將字符串和數字連接,因為是動態語言嘛.但在Python里有點詭異,如下:
#! /usr/bin/python
運行這行程序會出錯,提示你字符串和數字不能連接,於是只好用內置函數進行轉換
#! /usr/bin/python
#運行這行程序會出錯,提示你字符串和數字不能連接,於是只好用內置函數進行轉換a=2
c=str(a)+b
e=a+int(d)#How to print multiply valuesprint ("c is %s,e is %i" % (c,e))'''
* 用int和str函數將字符串和數字進行轉換
* 打印以#開頭,而不是習慣的//
* 打印多個參數的方式
#! /usr/bin/python
# -*- coding: utf8 -*-
#列表類似Javascript的數組,方便易用#定義元組word=['a','b','c','d','e','f','g']
#如何通過索引訪問元組裡的元素a=word[2]print ("a is: "+a)
b=word[1:3]print ("b is: ")print (b) # index 1 and 2 elements of word.c=word[:2]print ("c is: ")print (c) # index 0 and 1 elements of word.d=word[0:]print ("d is: ")print (d) # All elements of word.#元組可以合併e=word[:2]+word[2:]print ("e is: ")print (e) # All elements of word.f=word[-1]print ("f is: ")print (f) # The last elements of word.g=word[-4:-2]print ("g is: ")print (g) # index 3 and 4 elements of word.h=word[-2:]print ("h is: ")print (h) # The last two elements.i=word[:-2]print ("i is: ")print (i) # Everything except the last two charactersl=len(word)print ("Length of word is: "+ str(l))print ("Adds new element")
word.append('h')print (word)
#刪除元素del word[0]print (word)del word[1:3]print (word)
* 列表長度是動態的,可任意添加刪除元素.
* 用索引可以很方便訪問元素,甚至返回一個子列表
* 更多方法請參考Python的文檔'''
#! /usr/bin/python
x={'a':'aaa','b':'bbb','c':12}print (x['a'])print (x['b'])print (x['c'])
for key in x:
print ("Key is %s and value is %s" % (key,x[key]))
* 將他當Java的Map來用即可.
比起C/C++,Python處理字符串的方式實在太讓人感動了.把字符串當列表來用吧.
#! /usr/bin/python
word="abcdefg"
a=word[2]print ("a is: "+a)
b=word[1:3]print ("b is: "+b) # index 1 and 2 elements of word.c=word[:2]print ("c is: "+c) # index 0 and 1 elements of word.d=word[0:]print ("d is: "+d) # All elements of word.e=word[:2]+word[2:]print ("e is: "+e) # All elements of word.f=word[-1]print ("f is: "+f) # The last elements of word.g=word[-4:-2]print ("g is: "+g) # index 3 and 4 elements of word.h=word[-2:]print ("h is: "+h) # The last two elements.i=word[:-2]print ("i is: "+i) # Everything except the last two charactersl=len(word)print ("Length of word is: "+ str(l))
中文和英文的字符串長度是否一樣?
#! /usr/bin/python
# -*- coding: utf8 -*-
s=input("輸入你的中文名,按回車繼續");print ("你的名字是
l=len(s)print ("你中文名字的長度是:"+str(l))
類似Java,在python3里所有字符串都是unicode,所以長度一致.
6 條件和循環語句
#! /usr/bin/python#條件和循環語句
x=int(input("Please enter an integer:"))if x&0:
print ("Negative changed to zero")
elif x==0:
print ("Zero")
print ("More")
# Loops Lista = ['cat', 'window', 'defenestrate']for x in a:
print (x, len(x))
* 條件和循環語句
* 如何得到控制台輸入
#! /usr/bin/python
# -*- coding: utf8 -*- def sum(a,b):
return a+b
func = sum
r = func(5,6)print (r)
# 提供默認值def add(a,b=2):
return a+b
r=add(1)print (r)
r=add(1,5)print (r)
一個好用的函數
#! /usr/bin/python
# -*- coding: utf8 -*- # The range() functiona =range (1,10)for i in a:
a = range(-2,-11,-3) # The 3rd parameter stands for stepfor i in a:
Python 不用{}來控制程序結構,他強迫你用縮進來寫程序,使代碼清晰.
定義函數方便簡單
方便好用的range函數
8 異常處理
#! /usr/bin/pythons=input("Input your age:")if s =="":
raise Exception("Input must no be empty.")
i=int(s)except Exception as err:
print(err)finally: # Clean up action
print("Goodbye!")
9 文件處理
對比Java,python的文本處理再次讓人感動
#! /usr/bin/python
spath="D:/download/baa.txt"
f=open(spath,"w") # Opens file for writing.Creates this file doesn't exist.f.write("First line 1.\n")
f.writelines("First line 2.")
f=open(spath,"r") # Opens file for readingfor line in f:
print("每一行的數據是:%s"%line)
open的參數:r表示讀,w寫數據,在寫之前先清空文件內容,a打開並附加內容.
打開文件之後記得關閉
10 類和繼承
class Base:
def __init__(self):
self.data = []
def add(self, x):
self.data.append(x)
def addtwice(self, x):
self.add(x)
self.add(x)
# Child extends Baseclass Child(Base):
def plus(self,a,b):
return a+b
oChild =Child()
oChild.add("str1")print (oChild.data)print (oChild.plus(2,3))
* self:類似Java的this參數
每一個.py文件稱為一個module,module之間可以互相導入.請參看以下例子:
# a.pydef add_func(a,b):
return a+b
# b.pyfrom a import add_func # Also can be : import aprint ("Import add_func from module a")print ("Result of 1 plus 2 is: ")print (add_func(1,2))
# If using "import a" , then here should be "a.add_func"
module可以定義在包裡面.Python定義包的方式稍微有點古怪,假設我們有一個parent文件夾,該文件夾有一個child子文件夾.child中有一個module a.py . 如何讓Python知道這個文件層次結構?很簡單,每個目錄都放一個名為_init_.py 的文件.該文件內容可以為空.這個層次結構如下所示:
--__init_.py
-- __init_.py
那麼Python如何找到我們定義的module?在標準包sys中,path屬性記錄了Python的包路徑.你可以將之打印出來:
import sys
print(sys.path)
通常我們可以將module的包路徑放到環境變量PYTHONPATH中,該環境變量會自動添加到sys.path屬性.另一種方便的方法是編程中直接指定我們的module路徑到sys.path 中:
import sysimport os
sys.path.append(os.getcwd()+'\\parent\\child')
print(sys.path)
from a import add_func
print (sys.path)
print ("Import add_func from module a")print ("Result of 1 plus 2 is: ")print (add_func(1,2))
如何定義模塊和包
如何將模塊路徑添加到系統路徑,以便python找到它們
如何得到當前路徑
12 內建幫助手冊
對比C++,Java的突出進步是內建Javadoc機制,程序員可以通過閱讀Javadoc了解函數用法.Python也內建了一些方便函數以便程序員參考.
dir函數: 查看某個類/對象的方法. 如果有某個方法想不起來,請敲dir. 在idle里,試試 dir(list)
help函數: 詳細的類/對象介紹. 在idle里, 試試 help(list)
【上篇】【下篇】扫一扫体验手机阅读
解决python3 写入中文文本查看为乱码的问题!
<span type="1" blog_id="2087274" userid='
72篇文章,6W+人气,0粉丝
新西兰资深网工运维之道
¥51.00360人订阅
前百度高级工程师的架构高可用实战
¥51.00218人订阅
<span type="1" blog_id="2087274" userid='}

我要回帖

更多关于 python输出中文乱码 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信