import re
string = open('a.txt').read()
new_str = re.sub('[^a-zA-Z0-9\n\.]', ' ', string)
open('b.txt', 'w').write(new_str)
It will change every non alphanumeric char to white space.
Answer from NIlesh Sharma on Stack Overflowimport re
string = open('a.txt').read()
new_str = re.sub('[^a-zA-Z0-9\n\.]', ' ', string)
open('b.txt', 'w').write(new_str)
It will change every non alphanumeric char to white space.
I'm pretty new and I doubt this is very elegant at all, but one option would be to take your string(s) after reading them in and running them through string.translate() to strip out the punctuation. Here is the Python documentation for it for version 2.7 (which i think you're using).
As far as the actual code, it might be something like this (but maybe someone better than me can confirm/improve on it):
fileString.translate(None, string.punctuation)
where "fileString" is the string that your open(fp) read in. "None" is provided in place of a translation table (which would normally be used to actually change some characters into others), and the second parameter, string.punctuation (a Python string constant containing all the punctuation symbols) is a set of characters that will be deleted from your string.
In the event that the above doesn't work, you could modify it as follows:
inChars = string.punctuation
outChars = ['']*32
tranlateTable = maketrans(inChars, outChars)
fileString.translate(tranlateTable)
There are a couple of other answers to similar questions i found via a quick search. I'll link them here, too, in case you can get more from them.
Removing Punctuation From Python List Items
Remove all special characters, punctuation and spaces from string
Strip Specific Punctuation in Python 2.x
Finally, if what I've said is completely wrong please comment and i'll remove it so that others don't try what I've said and become frustrated.
Strip special characters from text file but keep new lines (Python) - Stack Overflow
python 3.x - Removing special characters from a text file - Stack Overflow
How to remove special characters from text file using python
regex - How to remove special characters except space from a file in python? - Stack Overflow
You can use the replace() method to to replace the \r\n substrings with an empty string.
with open(data, 'r', encoding='utf-8') as myfile:
with open('out.txt', 'a', encoding='utf-8') as output:
for line in myfile:
output.write(line.replace('\r\n', '').rstrip())
To remove special characters, such as newline characters (\n), from a text file in Python, you can use the replace() method of the str class. This method allows you to replace a specific character or string of characters with another character or string.
You can use this pattern, too, with regex:
import re
a = '''hello? there A-Z-R_T(,**), world, welcome to python.
this **should? the next line#followed- by@ an#other %million^ %%like $this.'''
for k in a.split("\n"):
print(re.sub(r"[^a-zA-Z0-9]+", ' ', k))
# Or:
# final = " ".join(re.findall(r"[a-zA-Z0-9]+", k))
# print(final)
Output:
hello there A Z R T world welcome to python
this should the next line followed by an other million like this
Edit:
Otherwise, you can store the final lines into a list:
final = [re.sub(r"[^a-zA-Z0-9]+", ' ', k) for k in a.split("\n")]
print(final)
Output:
['hello there A Z R T world welcome to python ', 'this should the next line followed by an other million like this ']
I think nfn neil answer is great...but i would just add a simple regex to remove all no words character,however it will consider underscore as part of the word
print re.sub(r'\W+', ' ', string)
>>> hello there A Z R_T world welcome to python
Hi, basically in my python script i'm using to learn I'm trying to select the title of a web page and then save a txt file with the same name of the title of the webpage. The only problem is, if the title of the web page contains special characters such as : then the name of the txt is truncated just before this special character. How do I avoid this problem? I'm fine with completely deleting the special character from the name of the txt file.
I have tried this, during the selection
p.title = title_page['title'] + contents.replace(':','')and also the same at the end, during the save of the file, but it doesn't work, it tells me contents is not defined in both situations. The problem is I cannot define it since the special character may be there but it may also NOT be there. How do I fix this?
Thanks