Remove a big list of of special characters [duplicate]

2024/7/4 14:22:14

I want to remove each of the following special characters from my documents:

symbols = {`,~,!,@,#,$,%,^,&,*,(,),_,-,+,=,{,[,],},|,\,:,;,",<,,,>,.,?,/}

The reason why I am not simply doing something like this:

document = re.sub(r'([^\s\w]|_)+', '', document)

is that in this way I remove also many (accented/special) letters in the case of documents written in languages such as Polish etc.

How can I remove each of the special characters above in one expression?

Answer

You can solve this without regular expressions by using str.replace():

symbols = {"`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "-", "+", "=", "{", "[", "]", "}", "|", "\\", ":", ";", "\"", "<", ",", ">", ".", "?", "/"}for c in symbols:document = document.replace(c, "")
https://en.xdnf.cn/q/120437.html

Related Q&A

How to change a html page from flask to Django [duplicate]

This question already has an answer here:How to specify URLs in Django templates?(1 answer)Closed 7 years ago.I am working on an app that requires changing a flask template to that of Django.How to ch…

How to get the text of Checkbuttons?

Checkbuttons gets generated dynamically and they are getting text from a python list. I need a logic for capturing selected checkbuttons text . As per my research everywhere they are returning the stat…

Working out an equation

Im trying to solve a differential equation numerically, and am writing an equation that will give me an array of the solution to each time point.import numpy as np import matplotlib.pylab as pltpi=np.p…

combine rows and add up value in dataframe

I got a dataframe(named table) with 6 columns labeled as [price1,price2,price3,time,type,volume]for type, I got Q and T, arranged like:QTQTTQNow I want to combine the rows with consecutive T and add up…

How to access a part of an element from a list?

import cv2 import os import glob import pandas as pd from pylibdmtx import pylibdmtx import xlsxwriter# co de for scanningimg_dir = "C:\\images" # Enter Directory of all images data_path = os…

How to get invisible data from website with BeautifulSoup

I need fiverr service delivery times but I could get just first packages(Basic) delivery time. How can I get second and third packages delivery time? Is there any chance I can get it without using Sel…

How to get rid of \n and in my json file

thanks for reading I am creating a json file as a result of an API that I am using. My issue is that the outcome gets has \h and in it and a .json file does not process the \n but keeps them, so the f…

Python code to calculate the maximal amount of baggage is allowed using recursive function

I am new to python and I have an assignment, I need to write a recursive function that takes two arguments (Weights, W), weights is the list of weights of baggage and W is the maximal weight a student …

How to flatten a nested dictionary? [duplicate]

This question already has answers here:Flatten nested dictionaries, compressing keys(32 answers)Closed 10 years ago.Is there a native function to flatten a nested dictionary to an output dictionary whe…

Find an element in a list of tuples in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 9 years ago.Improve…