shlex包括空字符串(shlex include empty strings)

编程入门 行业动态 更新时间:2024-10-23 01:49:36
shlex包括空字符串(shlex include empty strings) sample = ",," values = shlex.shlex(sample, posix=True) values.quotes = '"' values.whitespace = ',' values.whitespace_split = True received_output = list(values)

在上面的代码示例中,我想将["", "", ""]作为received_output的值,但received_output只是一个空列表[] 。 似乎没有关于如何接收这种预期行为的任何信息。

这适用于sample.split(',') ,但我更喜欢使用shlex,因为我有一个带有标记的复杂句子,如果是一个组的一部分(例如以下示例中的纬度,经度),则不应将其拆分。

另一个例子:

sample = '9267,BELMONT,KEELER,,62.4,35.2,10/01/2012,Weekday,"(41.93897000, -87.73212000)"' expected_output = ['9267', 'BELMONT', 'KEELER', '', '62.4', '35.2', '10/01/2012', 'Weekday', '(41.93897000, -87.73212000)'] retrieved_output = ['9267', 'BELMONT', 'KEELER', '62.4', '35.2', '10/01/2012', 'Weekday', '(41.93897000, -87.73212000)'] sample = ",," values = shlex.shlex(sample, posix=True) values.quotes = '"' values.whitespace = ',' values.whitespace_split = True received_output = list(values)

In the above code sample I would like to have ["", "", ""] as the value of received_output, but received_output is simply an empty list []. There does not seem to be any information regarding how to receive this expected behavior.

This works fine with sample.split(','), but I would prefer using shlex since I have complex sentences with tokens which should not be split up if part of a group (such as latitude, longitude in the following example).

Another example:

sample = '9267,BELMONT,KEELER,,62.4,35.2,10/01/2012,Weekday,"(41.93897000, -87.73212000)"' expected_output = ['9267', 'BELMONT', 'KEELER', '', '62.4', '35.2', '10/01/2012', 'Weekday', '(41.93897000, -87.73212000)'] retrieved_output = ['9267', 'BELMONT', 'KEELER', '62.4', '35.2', '10/01/2012', 'Weekday', '(41.93897000, -87.73212000)']

最满意答案

shlex文档声明:

即使引用,也无法解析空字符串。

如果要在输出中包含空字符串,则shlex库是作业的错误工具。

正如@PadraicCunningham在评论中指出的那样, csv (逗号分隔值)库应该可以正常工作:

>>> list(csv.reader(['9267,BELMONT,KEELER,,62.4,35.2,10/01/2012,Weekday,"(41.93897000, -87.73212000)"']))[0] ['9267', 'BELMONT', 'KEELER', '', '62.4', '35.2', '10/01/2012', 'Weekday', '(41.93897000, -87.73212000)'] >>> list(csv.reader([',,']))[0] ['', '', '']

The shlex docs state:

It’s not possible to parse empty strings, even if quoted.

If you want to include empty strings in your output, the shlex library is the wrong tool for the job.

As @PadraicCunningham points out in a comment, the csv (Comma Separated Values) library should work fine for this:

>>> list(csv.reader(['9267,BELMONT,KEELER,,62.4,35.2,10/01/2012,Weekday,"(41.93897000, -87.73212000)"']))[0] ['9267', 'BELMONT', 'KEELER', '', '62.4', '35.2', '10/01/2012', 'Weekday', '(41.93897000, -87.73212000)'] >>> list(csv.reader([',,']))[0] ['', '', '']

更多推荐

本文发布于:2023-08-06 21:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454982.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:空字符串   shlex   include   strings   empty

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!