如何用python中的空格替换所有这些特殊字符? [英] How to replace all those Special Characters with white spaces in python?

查看:122
本文介绍了如何用python中的空格替换所有这些特殊字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在python中用空格替换所有这些特殊字符?

How to replace all those special characters with white spaces in python ?

我有一个公司名称列表...

I have a list of names of a company . . .

例如:-[myfiles.txt]

Ex:-[myfiles.txt]

我的公司.INC

老酒列兵

主脑有限公司

顶点实验室有限公司"

印度新公司"

印美 pvt/ltd

这里,按照上面的例子...我需要文件 myfiles.txt 中的所有特殊字符 [-,",/,.] 必须替换为单个空格并保存到另一个文本文件 myfiles1.txt.

Here, as per the above example . . . I need all the special characters[-,",/,.] in the file myfiles.txt must be replaced with a single white space and saved into another text file myfiles1.txt.

有人可以帮我吗?

推荐答案

假设您想更改所有非字母数字的内容,您可以在命令行上执行此操作:

Assuming you mean to change everything non-alphanumeric, you can do this on the command line:

cat foo.txt | sed "s/[^A-Za-z0-99]/ /g" > bar.txt

或者在 Python 中使用 re 模块:

Or in Python with the re module:

import re
original_string = open('foo.txt').read()
new_string = re.sub('[^a-zA-Z0-9\n\.]', ' ', original_string)
open('bar.txt', 'w').write(new_string)

这篇关于如何用python中的空格替换所有这些特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆