# -*- coding:utf-8 -*- import sys,os txta = open('a.txt','r') str = '' for line in txta: str += line.strip().decode('utf-8') txta.close() for word in str: print word.encode('utf-8') 直接输出,是会乱码的,得先解码,再编码. 参考网址:http://blog.csdn.net/devil_2009/article/de…
python中字符串可以(且仅可以)使用成对的单引号.双引号.三个双引号(文档字符串)包围: 'this is a book' "this is a book" """this is a book""" 可在单引号包围的字符串中包含双引号,三引号等,但不能包含单引号自身(需转义) 'this is a" book' 'this is a"" book' 'this is a""…
今天本来打算写个程序,替换字符串中固定的一个字符:将<全部替换成回车'\n' 于是,我写成这样 s='sdjj<ddd<denj,>' for x in s: if x=='<': x='\n' print(s) 然后输出还是 'sdjj<ddd<denj,>' 然后我就很纳闷,于是乎我又写成了这样 s='sdjj<ddd<denj,>' ss=list(s) for x in ss: if x=='<': x='\n' print(…