简体   繁体   中英

Using Regex to add quotations to delimited list

I have a copied list I want to convert to a pandas data frame. However, when I copy the test I get raw values instead of strings. ie foo, bar, buzz instead of " foo", "bar", "buzz" the list then ends with numbers that I do not want to surround with quotations. I was able to select the text between 2 commas including a comma using ,[^,]+ but I do not want to include the comma because I want to replace the text inside with its quotation wrapped version. I cannot simply exclude numbers from the regex ie ,[^,0-9]+ because some of the categories have numbers embedded in them (such as iPhone-6s or Toyota Rav4). How would I accomplish this?

Assuming your CSV input be in a single string, you may try:

inp = "foo, bar, buzz, make-model"
output = re.sub(r'\b([\w-]+)\b', '"\\1"', inp)
print(output)

This prints:

"foo", "bar", "buzz", "make-model"

You can try ast.liter_eval() like so:

import ast

my_string = "[1, 2, 'some_string', {'some':'dict'}]"
my_list = ast.literal_eval(my_string)

my_list then becomes:

[1, 2, 'some_string', {'some': 'dict'}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM