简体   繁体   English

如何使用列表推导将逗号替换为包含字符串和数字的列表中的转义逗号

[英]How can I use list comprehension to replace a comma with an escaped comma in a list that contains strings and numbers

I have a tuple that contains elements that are both strings and numbers, and I am trying to replace a comma that exists in one of the elements of the tuple with an escaped comma, eg the input looks like the following 我有一个包含字符串和数字元素的元组,我试图用转义的逗号替换存在于元组的一个元素中的逗号,例如输入如下所示

('', 'i_like_cats', 'I like, cookies', 10319708L, "* / Item ID='10319708'", 'i_wish_was_an_oscar_meyer_weiner',
0.101021321)

and what I want as output is 我想要的输出是什么

('', 'i_like_cats', 'I like\, cookies', 10319708L, "* / Item ID='10319708'", 'i_wish_was_an_oscar_meyer_weiner',
0.101021321)

What I want to do is replace the , after like with /, because I'm outputting the content to a csv file, so when the next step of the pipeline reads the file it splits the file at the comma between 'like' and 'cookies' even though I don't want it to. 我想要做的是用/之后替换,因为我正在将内容输出到csv文件,所以当管道的下一步读取文件时,它会在'like'和'之间用逗号分隔文件饼干'即使我不想要它。 I am unable to modify the code of the downstream part of the pipeline so I can't switch to something like a semicolon or tab delimited file, and I can't change. 我无法修改管道下游部分的代码,因此我无法切换到分号或制表符分隔文件,我无法更改。

What I've tried to do is use list comprehension to solve this as follows 我试图做的是使用列表理解来解决这个问题如下

line = map(lambda x: str.replace(x, '"', '\"'), line)

but that generates a type error indicating that replace requires a string object but it received a long. 但是这会生成一个类型错误,指示replace需要一个字符串对象,但它收到了一个long。

The only solution I can think of is to break the tuple down into it's individual elements, modify the element that contains the comma, and then build a new tuple and append each of the elements back on, but it seems like there has to be a more pythonic way to do that. 我能想到的唯一解决方案是将元组分解为单个元素,修改包含逗号的元素,然后构建一个新元组并重新附加每个元素,但似乎必须有一个更多的pythonic方式来做到这一点。

Thanks! 谢谢!

I think list comprehension works the best for apply a rule for every element; 我认为列表理解最适合为每个元素应用规则; if you know which single string you would like to change, why can't you just change that single one? 如果您知道要更改哪个单个字符串,为什么不能只更改那个单个字符串?

If you want to change all comma into escaped comma, probably try this out: 如果你想将所有逗号更改为转义逗号,可以试试这个:

strtuple = ('', 'i_like_cats', 'I like, cookies', 10319708, "* / Item ID='10319708'", 'i_wish_was_an_oscar_meyer_weiner',
0.101021321)
converted_strlist = [i.replace(',', '\\,') if isinstance(i, str) else i for i in strtuple]

You might want to verify an element is a str instance before calling replace on it. 您可能希望在调用replace之前验证元素是否为str实例。

Something like: 就像是:

    data = (
        '',
        'i_like_cats',
        'I like, cookies',
        10319708L,
        "* / Item ID='10319708'",
        'i_wish_was_an_oscar_meyer_weiner',
        0.101021321,
    )

    line = [
        x.replace(",", "\,") if isinstance(x, str) else x for x in data
    ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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