shlex包括空字符串(shlex include empty strings)
在上面的代码示例中,我想将["", "", ""]作为received_output的值,但received_output只是一个空列表[] 。 似乎没有关于如何接收这种预期行为的任何信息。
这适用于sample.split(',') ,但我更喜欢使用shlex,因为我有一个带有标记的复杂句子,如果是一个组的一部分(例如以下示例中的纬度,经度),则不应将其拆分。
另一个例子:
sample = '9267,BELMOT,KEELER,,62.4,5.2,10/01/2012,Weekday,"(41.9897000, -87.7212000)"' expected_output = ['9267', 'BELMOT', 'KEELER', '', '62.4', '5.2', '10/01/2012', 'Weekday', '(41.9897000, -87.7212000)'] retrieved_output = ['9267', 'BELMOT', 'KEELER', '62.4', '5.2', '10/01/2012', 'Weekday', '(41.9897000, -87.7212000)'] 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,BELMOT,KEELER,,62.4,5.2,10/01/2012,Weekday,"(41.9897000, -87.7212000)"' expected_output = ['9267', 'BELMOT', 'KEELER', '', '62.4', '5.2', '10/01/2012', 'Weekday', '(41.9897000, -87.7212000)'] retrieved_output = ['9267', 'BELMOT', 'KEELER', '62.4', '5.2', '10/01/2012', 'Weekday', '(41.9897000, -87.7212000)']最满意答案
shlex文档声明:
即使引用,也无法解析空字符串。
如果要在输出中包含空字符串,则shlex库是作业的错误工具。
正如@PadraicCunningham在评论中指出的那样, csv (逗号分隔值)库应该可以正常工作:
>>> list(csv.reader(['9267,BELMOT,KEELER,,62.4,5.2,10/01/2012,Weekday,"(41.9897000, -87.7212000)"']))[0] ['9267', 'BELMOT', 'KEELER', '', '62.4', '5.2', '10/01/2012', 'Weekday', '(41.9897000, -87.7212000)'] >>> 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,BELMOT,KEELER,,62.4,5.2,10/01/2012,Weekday,"(41.9897000, -87.7212000)"']))[0] ['9267', 'BELMOT', 'KEELER', '', '62.4', '5.2', '10/01/2012', 'Weekday', '(41.9897000, -87.7212000)'] >>> list(csv.reader([',,']))[0] ['', '', '']#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
推荐阅读
留言与评论(共有 16 条评论) |
本站网友 张成源 | 13分钟前 发表 |
BELMOT | |
本站网友 小儿脑炎 | 5分钟前 发表 |
62.4 | |
本站网友 唐山凤凰妇科 | 24分钟前 发表 |
经度) | |
本站网友 手掌有痣 | 28分钟前 发表 |
5.2 | |
本站网友 力帆置业 | 18分钟前 发表 |
'(41.9897000 | |
本站网友 cntv加速器 | 22分钟前 发表 |
'10/01/2012' | |
本站网友 狗狗搜索 | 5分钟前 发表 |
-87.7212000)'] >>> list(csv.reader([' | |
本站网友 背部按摩手法 | 24分钟前 发表 |
则不应将其拆分 | |
本站网友 美容绣眉 | 20分钟前 发表 |
-87.7212000)'] >>> list(csv.reader([' | |
本站网友 分区表医生 | 8分钟前 发表 |
本站网友 误判心理学 | 23分钟前 发表 |
'5.2' | |
本站网友 横店租房 | 15分钟前 发表 |
'KEELER' | |
本站网友 姓王的好名字 | 24分钟前 发表 |
10/01/2012 | |
本站网友 伯克纳 | 17分钟前 发表 |
'KEELER' | |
本站网友 酵母是什么意思 | 1分钟前 发表 |
""] as the value of received_output |