top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration
Print Preview

Find and Replace Simplification for Python code

0 votes

I have some code that I want to simplify. I know that a for-loop would work well, but can I make re.sub perform all of the below tasks at once, or can I write this in a way that is more efficient than using a for-loop?

DATA = re.sub(',', '', 'DATA')
DATA = re.sub(''', '', 'DATA')
DATA = re.sub('(', '', 'DATA')
DATA = re.sub(')', '', 'DATA')

posted Jul 19, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button
Try DATA = re.sub(r'[(,\)]', '', 'DATA')

2 Answers

+1 vote
Best answer

I don't think you intended to put DATA in quotes on the right hand side. That makes it literally the string D A T A, so all those replacements are no-ops, and you could simplify it to:


But that's probably not what you wanted.

My prediction is that this will be by far the most efficient way to do what you are trying to do:

py> DATA = "Hello, 'World'()"
py> DATA.translate(dict.fromkeys(ord(c) for c in ",'()"))
'Hello World'

That's in Python 3 -- in Python 2, using translate will still probably be the fastest, but you'll need to call it like this:

import string
DATA.translate(string.maketrans("", ""), ",'()")

I also expect that the string replace() method will be second fastest, and re.sub will be the slowest, by a very long way.

As a general rule, you should avoiding using regexes unless the text you are searching for actually contains a regular expression of some kind. If it's merely a literal character or substring, standard string methods will probably be faster.

a tip for you:

- don't escape quotes unless you don't need to, use the other quote.
s = ''' # No, don't do this!
s = "'" # Better!
and vice versa.
answer Jul 19, 2013 by anonymous
0 votes

If your actual use-case is this simple, you might want to use one of the built-in string functions such as strip() or translate().

answer Jul 19, 2013 by anonymous
Similar Questions
+3 votes

I need python code snippet for LTE/UMTS for automation testing.Example of scenario looking for LTE attach ,detach, MO/MT call .
I am looking for UE side scripts.

+3 votes

Here is what I have tried:

root@secure [~]# which python3

root@secure [~]# which pip

root@secure [~]# yum install pip3
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base:
 * epel:
 * extras:
 * remi:
 * updates:
Setting up Install Process
No package pip3 available.
Error: Nothing to do
+1 vote

Let's say I want to compare two csv files: file A and file B. They are both similarly built - the first column has product IDs (one product per row) and the columns provide some stats about the products such as sales in # and $.

I want to compare these files - see which product IDs appear in the first column of file A and not in B, and which in B and not A.
Finally, it would be very great if the result could be written into two new CSV files - one product ID per row in the first column. (no other data in the other columns needed)

This is the script I tried:

import csv

#open CSV's and read first column with product IDs into variables pointing to lists
A = [line.split(',')[0] for line in open('Afile.csv')]
B = [line.split(',')[0] for line in open('Bfile.csv')]

#create variables pointing to lists with unique product IDs in A and B respectively 
inAnotB = list(set(A)-set(B))
inBnotA = list(set(B)-set(A))

print inAnotB
print inBnotA

c = csv.writer(open("inAnotB.csv", "wb"))

d = csv.writer(open("inBnotA.csv", "wb"))

print "done!" 

But it doesn't produce the required results.
It prints IDs in this format:


and nothing to the csv files.

+2 votes

I am trying to learn Django. My initial exercise seems fine. I want to create an API with REST on Django for an interactive Python code. REST framework on Django I am understanding more or less.

I am looking for a simple example to start with. I am using Python 2.7+ on MS-Windows 7 Professional.

If any one may kindly suggest.

+2 votes

I am searching how to get status code in selinium (with python), but unable to find it . My aim is to scroll a page and after each scroll i want to find the status so that on bad status I can exit my code.

My code

 for i in range(0,60):
 driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
 # I want status code here"

Useful Links with Similar Problem
Contact Us
+91 9880187415
#470/147, 3rd Floor, 5th Main,
HSR Layout Sector 7,
Bangalore - 560102,
Karnataka INDIA.