0

How can I transform the string like this, I need to convert two double quotes to a \".

"Alexandra ""Alex""",Menendez,[email protected],Miami,1
["Alexandra \"Alex\"", "Menendez", "[email protected]", "Miami", "1"]

the return should be a list and that's where I am landing into a problem. This is what I came up with

def parseCsv(sentence):
    result = []
    new_result = []
    sent_1 = list(sentence)
    new_str = ""
    new_w = ""

    for s in sent_1:
        if s != ',':
            new_str += " ".join(s)
        elif new_str:
            result.append(new_str)
            new_str = ""
    result.append(new_str)

    for i, w in enumerate(result):
        if w.startswith("\"") or w.endswith("\""):
            new_w += result[i]
            if new_w.startswith("\"") and new_w.endswith("\""):
                x = new_w.replace('""', r'\"')
                new_result.append(x)
                new_w = ""
        else:
            new_result.append(w)
    return new_result

What I get back instead is

['"Alexandra \\"Alex\\""', 'Menendez', '[email protected]', 'Miami', '1']
2
  • can you try doing a for loop to print out the contents? Commented Sep 6, 2017 at 5:03
  • by the way you can use Python's string.split(',') to form a list of words separated by commas Commented Sep 6, 2017 at 5:08

2 Answers 2

4

You don't have to do anything. The csv module will load your data correctly:

import csv
from io import StringIO

infile = StringIO('"Alexandra ""Alex""",Menendez,[email protected],Miami,1')
reader = csv.reader(infile)
for row in reader:
    print(row)

# output:
# ['Alexandra "Alex"', 'Menendez', '[email protected]', 'Miami', '1']
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks but I would want to do it without csv library.
@Arman Why don't you want to use the cvs module? With this you can then easily add each row to a list or dict in the for loop.What is your end goal for this data?
May be I didn't express what my end goal is! I could have some this like SELECT * FROM table WHERE name LIKE "Alex;" The need here is I need to get the double quotes too and if there are two double quotes I need to escape it with a \". I would want to do it without a CSV library. Also this is what i want ["Alexandra \"Alex\"", "Menendez", "[email protected]", "Miami", "1"] it's different from what you get when you use csv.
@Arman What you have there is clearly csv data, so you should use the csv module to parse it. Then if your database module forces you to manually escape anything, you should find a different, more secure database module that escapes stuff automatically.
Well, let's not get to the database module even. If you have any recommendation on how to parse the string to what I need, it would be helpful.
|
0

Say, after converting your sentence into list and iterating over every elements, do the following regsub

Consider 'a' is an element from your list

a= "\"Alexadra \"\"Alex\"\"\""

print(a)

y=re.sub('\"{2}', '\\\"', a)

y will replace your double quotes with \" i.e a slash with single quote

If only single quote is required try

z=re.sub('\"{2}', '\"', a)

1 Comment

Or use the csv module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.