Python将字符串内\n去掉用什么方法,python字符串,从文件中读出的一个汉字字
Python将字符串内\n去掉用什么方法,python字符串,从文件中读出的一个汉字字
从文件中读出的一个汉字字符串
用这个句话判断是否包含回车:
python if '\n' in mystring: print type(mystring) #<type 'unicode'> mystring.replace('\n', '') #does not work mystring.replace(u'\n', u'') #does not work mystring.encode("gbk") mystring.replace("\n", "") #does not work mystring.encode("utf-8") mystring.replace("\n", "") #does not work mystring.encode("ascii") mystring.replace("\n", "") #UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-20 print mystring
这该如何是好?请指教!
字符串的 replace 方法不会修改原字符串的内容
尝试
a = "woshishazi\n!!!" a = a.replace('\n', '')
我想到的是,既然是从文件读入的,那为什么不用strip呢?
python file = open('file.txt', 'rt') for line in file: line = line.strip()
编橙之家文章,
相关内容
- 谁能详细解释一下tornado异步非阻塞框架,tornado异步,
- python大师帮我看看源码中def自定义函数有什么作用,
- python正则替换字符串中间字符表达式怎么写,python字符
- python微信平台自动回复信息如何写,python自动回复,就是
- Python遍历Geojson数据坐标应用问题,pythongeojson,有一大推
- Python文件同应用打包方法是?,,如Goagent中好像把
- 求Python方法检测变量名,python检测变量,如题,除了用
- 不小心删除了pythonwin,重装之后提示不是有效的Win32应用
- 求python删除相连空格并保留唯一的方法,python空格,py
- Python HTTP服务器载入指定url失败图片不能正常显示怎么
评论关闭