{"id":559,"date":"2014-02-12T07:10:35","date_gmt":"2014-02-12T15:10:35","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=559"},"modified":"2014-02-12T07:10:35","modified_gmt":"2014-02-12T15:10:35","slug":"python-split-using-space-vs-none-separator","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/python-split-using-space-vs-none-separator\/","title":{"rendered":"Python Split Using Space vs None Separator"},"content":{"rendered":"
Quick reminder of how to split a \"ls -l\" listing. It is problematic because the last field which is the directory or file name can have spaces itself, so splitting on spaces which is the only option here still need a bit of additional work.<\/p>\n
\r\nimport config\r\nimport subprocess\r\n\r\n## First experiment doing manual manipulation of the fields. \r\n## Basically split on all spaces and then assemble the file name.\r\ndef dir_listing():\r\n ls_lines = subprocess.check_output(['ls', '-l']).splitlines()\r\n ls_arr= []\r\n for item in ls_lines:\r\n if not "total" in item:\r\n f = item.split()\r\n ## fname = item.join(str(v) for v in item.index if v > 7)\r\n fname = ""\r\n for idx,val in enumerate(f):\r\n if idx > 7:\r\n fname = fname + str(val) + " "\r\n fname = fname[:-1]\r\n ls_fields = [f[0],f[1],f[2],f[3],f[4],f[5]+"-"+f[6]+"-"+f[7],fname]\r\n ls_arr.append(ls_fields)\r\n return ls_arr\r\n\r\n## Second attempt is split on spaces with a max split defined. \r\n## This sounds like the obvious way to do this but I needed to use "None" and not " "\r\n## as shown below.\r\ndef dir_listing_optimized():\r\n ls_lines = subprocess.check_output(['ls', '-l']).splitlines()\r\n ls_arr= []\r\n for item in ls_lines:\r\n if not "total" in item:\r\n ## None will use arbitrary strings of whitespace characters (space, tab, newline, return, formfeed)\r\n ## When using split(" ",8) I had more separation than I expected and therefor my last field \r\n ## was not working right.\r\n f = item.split(None,8)\r\n ls_arr.append(f)\r\n return ls_arr\r\n\r\nfor dir in dir_listing():\r\n print dir\r\n\r\nfor dir in dir_listing_optimized():\r\n print dir\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"Quick reminder of how to split a “ls -l” listing. It is problematic because the last field which is the<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-559","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/559","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/comments?post=559"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/559\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}