Remove all characters in a row with re.sub python

Notsoprosql :

I am trying to edit the data to replace ALL characters before a set point. Currently I have got this far with Python but this gives me a error "TypeError: 'str' object is not callable"

Here is the data set before and what I am trying to achieve:

Header 1,Header 2,Header 3,Header 4,Header 5 
1,2,3,4,DESCRIPTION
1,2,3,4,DEffffSCRIPTION
1,2,3,4,aaaaDESCRIPTION
1,2,3,4,<>DESCRIPTION

Here is what I am trying to achieve:

Header 1,Header 2,Header 3,Header 4,Header 5 
1,2,3,4,SCRIPTION
1,2,3,4,SCRIPTION
1,2,3,4,SCRIPTION
1,2,3,4,SCRIPTION

If you can help fix the TypeError in the re.sub or if you can help overall that would be great thank you.

Here is my python code.

import csv
import re

with open('2.csv', "r") as inFile:
    reader = csv.reader(inFile)
    for row in reader:
        row[4](re.sub(r'.*SC', r'SC.*', row[4]))
Alan :

I think you just have a small syntax error on this line.

row[4](re.sub(r'.*SC', r'SC.*', row[4]))

row[4] is a string, so you can't call re.sub on it like that. You need to store the output of re.sub in a variable.

subbed_row = re.sub(r'.*SC', r'SC.*', row[4])

And from there figure out how you want to write that back into the csv.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=383549&siteId=1