在python中拆分并剥离空格

时间:2016-11-12 20:45:16

标签: python split

我正在学习Python,目前正在阅读文件,拆分行然后打印特定元素。我在分割多次时遇到麻烦。我正在处理的文件有许多看起来像这样的行

c0_g1_i1|m.1    gi|74665200|sp|Q9HGP0.1|PVG4_SCHPO      100.00  372     0       0       1       372     1       372     0.0       754

我试图将它拆分,首先是制表符和换行符" / t / n",然后用|拆分元素,我尝试过.split和.strip并且没有太多运气。我想也许,如果我只是在单行上工作,我可以理解,然后将其修改为一个可以访问文件的循环

blast_out = ("c0_g1_i1|m.1    gi|74665200|sp|Q9HGP0.1|PVG4_SCHPO      100.00  372     0       0       1       372     1       372     0.0       754")
fields = blast_out.strip(' \t\r\n').split()
subFields = fields.split("|")
print(fields)
print(subFields)

打印(字段)

['c0_g1_i1|m.1', 'gi|74665200|sp|Q9HGP0.1|PVG4_SCHPO', '100.00', '372', '0', '0', '1', '372', '1', '372', '0.0', '754']

print(subFields)生成错误

subFields = fields.split('|')
AttributeError: 'list' object has no attribute 'split'

这就是我尝试剥离空白和标签,然后拆分|但它似乎没有做任何事情。最终我从这个单字符串输出的所需输出将是

c0_g1_i1 m.1 Q9HGP0.1 100.0

1 个答案:

答案 0 :(得分:2)

现在有一个单独的字符串列表。看起来好像输入格式编码嵌套列表;外部格式由空格分隔,内部由npm run build deep-playground-prototype@2016.3.10 build C:\Users\Arash\Desktop\AUTUMN16\Computational Intelligence\TensorFlow-Playground npm run prep && npm run build-js && npm run build-css && npm run build-html deep-playground-prototype@2016.3.10 prep C:\Users\Arash\Desktop\AUTUMN16\Computational Intelligence\TensorFlow-Playground typings install && mkdir -p dist && cat node_modules/material-design-lite/material.min.js node_modules/d3/d3.min.js node_modules/seedrandom/seedrandom.min.js > dist/lib.js typings WARN deprecated 9/7/2016, 5:27:44 AM: "registry:dt/d3#0.0.0+20160315011939" has been deprecated └── d3 (ambient) 'cat' is not recognized as an internal or external command, operable program or batch file. npm ERR! Windows_NT 10.0.10586 npm ERR! argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "run" "prep" npm ERR! node v4.5.0 npm ERR! npm v2.15.9 npm ERR! code ELIFECYCLE npm ERR! deep-playground-prototype@2016.3.10 prep: typings install && mkdir -p dist && cat node_modules/material-design-lite/material.min.js node_modules/d3/d3.min.js node_modules/seedrandom/seedrandom.min.js > dist/lib.js npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the deep-playground-prototype@2016.3.10 prep script 'typings install && mkdir -p dist && cat node_modules/material-design-lite/material.min.js node_modules/d3/d3.min.js node_modules/seedrandom/seedrandom.min.js > dist/lib.js'. npm ERR! This is most likely a problem with the deep-playground-prototype package, npm ERR! not with npm itself. npm ERR! Tell the author that this fails on your system: npm ERR! typings install && mkdir -p dist && cat node_modules/material-design-lite/material.min.js node_modules/d3/d3.min.js node_modules/seedrandom/seedrandom.min.js > dist/lib.js npm ERR! You can get information on how to open an issue for this project with: npm ERR! npm bugs deep-playground-prototype npm ERR! Or if that isn't available, you can get their info via: npm ERR! npm ERR! npm owner ls deep-playground-prototype npm ERR! There is likely additional logging output above. npm ERR! Please include the following file with any support request: npm ERR! C:\Users\Arash\Desktop\AUTUMN16\Computational Intelligence\TensorFlow-Playground\npm-debug.log npm ERR! Windows_NT 10.0.10586 npm ERR! argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "run" "build" npm ERR! node v4.5.0 npm ERR! npm v2.15.9 npm ERR! code ELIFECYCLE npm ERR! deep-playground-prototype@2016.3.10 build: npm run prep && npm run build-js && npm run build-css && npm run build-html npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the deep-playground-prototype@2016.3.10 build script 'npm run prep && npm run build-js && npm run build-css && npm run build-html'. npm ERR! This is most likely a problem with the deep-playground-prototype package, npm ERR! not with npm itself. npm ERR! Tell the author that this fails on your system: npm ERR! npm run prep && npm run build-js && npm run build-css && npm run build-html npm ERR! You can get information on how to open an issue for this project with: npm ERR! npm bugs deep-playground-prototype npm ERR! Or if that isn't available, you can get their info via: npm ERR! npm ERR! npm owner ls deep-playground-prototype npm ERR! There is likely additional logging output above. npm ERR! Please include the following file with any support request: npm ERR! C:\Users\Arash\Desktop\AUTUMN16\Computational Intelligence\TensorFlow-Playground\npm-debug.log 个字符组成。

您可以拆分外部字符串,然后在列表解析中再次拆分每个结果元素:

typings install && mkdir -p dist && cat node_modules/material-design-lite/material.min.js node_modules/d3/d3.min.js node_modules/seedrandom/seedrandom.min.js > dist/lib.js

请注意npm run prep && npm run build-js && npm run build-css && npm run build-html完全是多余的,|调用(没有参数或[item.split('|') for item in blast_out.split()] 作为第一个参数)已经删除了前导空格和尾随空格。

如果你期望一个平面列表,你可以在理解中添加另一个循环:

str.strip()

如果内部列表中的项目数是可变的,那么前者将是更可取的;找到嵌套列表的第一个或最后一个元素比在平面列表中找到每个以空格分隔的部分开始或结束更容易。

您可以使用以下两个表达式之一提取您给定示例的最终值,具体取决于您选择的变体:

str.split()

None

演示:

[value for item in blast_out.split() for value in item.split('|')]