How to Remove single character from a Mixed string In Python
<p><a href="http://i.stack.imgur.com/S55ee.png" rel="nofollow"><img src="http://i.stack.imgur.com/S55ee.png" alt="Table "></a></p>

<p>I have one table (Please Ref Image) In this Table I want to remove "A" char from  each Row How can I do in Python.</p>

<p>Below is my code using <code>regexe_replace</code> but code is not optimised I want optimised code</p>

<pre><code> def re(s):
      return regexp_replace(s, "A", "").cast("Integer")

    finalDF = finalD.select(re(col("C0")).alias("C0"),col("C1"),
                        re(col("C2")).alias("C2"),
                        re(col("C3")).alias("C3"),col("C4"),
                        re(col("C5")).alias("C5"),
                        re(col("C6")).alias("C6"),col("C7"),
                        re(col("C8")).alias("C8"),
                        re(col("C9")).alias("C9"),col("C10"),
                        re(col("C11")).alias("C11"),col("C12"),
                        re(col("C13")).alias("C13"),
                        re(col("C14")).alias("C14"),col("C15"),
                        re(col("C16")).alias("16"),col("C17"),
                        re(col("C18")).alias("18"),
                        re(col("C19")).alias("C19"),col("Label"))
    finalDF.show(2)
</code></pre>

<p>Thank you in Advance.</p>

<p>Why regex? Regex will be over kill.</p>

<p>If you have data in format you have given, then use <strong>replace function</strong> as below:</p>

<p><strong>Content of master.csv:</strong></p>

<pre><code>A11| 6|A34|A43|
A11| 6|A35|A44|
</code></pre>

<p><strong>Code :</strong></p>

<pre><code>with open('master.csv','r') as fh:
    for line in fh.readlines():
        print "Before - ",line
        line = line.replace('A','')
        print "After - ", line
        print "---------------------------"
</code></pre>

<p>Output:</p>

<pre><code>C:\Users\dinesh_pundkar\Desktop&gt;python c.py
Before -  A11| 6|A34|A43|
After -  11| 6|34|43|
---------------------------
Before -  A11| 6|A35|A44|
After -  11| 6|35|44|
---------------------------
</code></pre>

<p><strong>Code with replacing 'A' from complete data in in one shot (without going line by line)</strong></p>

<pre><code>with open("master.csv",'r') as fh:
    data = fh.read()
    data_after_remove = data.replace('A','')
    print "Before remove ..."
    print data
    print "After remove ..."
    print data_after_remove
</code></pre>

<p><strong>Output:</strong></p>

<pre><code>C:\Users\dinesh_pundkar\Desktop&gt;python c.py
Before remove...
A11| 6|A34|A43|
A11| 6|A35|A44|
After remove ...
11| 6|34|43|
11| 6|35|44|

C:\Users\dinesh_pundkar\Desktop&gt;
</code></pre>

python optimization replace