os.system输出到字典

时间:2017-06-28 07:59:06

标签: python-2.7

我希望public $to = [];是我的关键,NC_008934.1是我的价值。不幸的是,以下代码无效

更新

Glypta fumiferanae ichnovirus segment B18, complete sequence

错误

from subprocess import Popen, PIPE

p = Popen(
        ["find .  -name \"*.fna\" -exec  grep \">\" '{}' \; | cut -d '|' -f 4,5"],
        stdout=PIPE,
        stderr=PIPE)
result, err = p.communicate()
if p.returncode != 0:
    raise IOError(err)
names = result.strip()
#names has many of this strings NC_008934.1| Glypta fumiferanae ichnovirus segment B18, complete sequence

names_dict = {n[0] : n[1] for n in (nameline.split("|") for nameline in namelines)}
print "!!!", names_dict

我错过了什么?

2 个答案:

答案 0 :(得分:0)

以下适用于我:

case MOVE_SUB_MENU: {  
  let newCustomMenus = state.mainMenus ? [...state.mainMenus] : []
  let newSubMenus = [...newCustomMenus[action.mainMenuIndex].sub_button]

  console.log(newSubMenus);

  if (action.to || action.to === 0) {
    newSubMenus.splice(action.to, 0, newSubMenus.splice(action.from, 1)[0])
    newCustomMenus[action.mainMenuIndex].sub_button = newSubMenus
  } else {
    newSubMenus.splice(action.from, 1)
    newCustomMenus[action.mainMenuIndex].sub_button = newSubMenus
  }

  console.log(newSubMenus); // Check changed SubMenus
  console.log(newCustomMenus); // Check changed CustomMenus

  return Object.assign({}, state, { mainMenus: newCustomMenus })
}

修改

根据您的评论,您的nameline1 = "NC_008934.1| Glypta fumiferanae ichnovirus segment B18, complete sequence" nameline2 = "NC_008934.2| Glypta fumiferanae ichnovirus segment B18, complete sequence 2" namelines = [nameline1, nameline2] names_dict = {n[0] : n[1] for n in (nameline.split("|") for nameline in namelines)} 调用的输出似乎不是您认为的:您获得shell命令的返回码,它是一个int,而不是它的标准输出。您可以使用os.system模块获得更多成功:

subprocess

为帮助您根据需要进行调整,请参阅以下链接:

请特别注意在上一个链接中使用from subprocess import Popen, PIPE p = Popen( ["find . -name \"*.fna\" -exec grep \">\" '{}' \; | cut -d '|' -f 4,5"], stdout=PIPE, stderr=PIPE, shell=True) result, err = p.communicate() if p.returncode != 0: raise IOError(err) namelines = result.decode("utf-8").strip().split("\n") 的警告:如果命令行不完全在您的控制范围内,请不要使用此功能。

此外,您似乎正在解析fasta文件。这可以使用各种python库来完成。你可以在这里看到一些测试:https://bioinformatics.stackexchange.com/a/380/292

答案 1 :(得分:0)

当您使用for n in names迭代字符串时,您将names中的每个字符,您似乎想要遍历行< {em} of names,您可以使用splitlines()

执行此操作

除此之外,你的最后一行在语法上是不正确的,你可能希望strip()键和值来删除任何尾随或前导空格。

试试这个:

names_dict = {pair[0].strip() : pair[1].strip() for pair in (line.split("|") for line in names.splitlines())}

它对我有用:

In [1]: names = """NC_008934.1| Glypta fumiferanae ichnovirus segment B18, complete sequence
   ...: NC_008934.2| Glypta fumiferanae ichnovirus segment B18, complete sequence 2
   ...: NC_008934.3| Glypta fumiferanae ichnovirus segment B18, complete sequence 3
   ...: NC_008934.4| Glypta fumiferanae ichnovirus segment B18, complete sequence 4"""

In [2]: names_dict = {pair[0].strip() : pair[1].strip() for pair in (line.split("|") for line in names.splitlines())}

In [3]: names_dict
Out[3]: 
{'NC_008934.1': 'Glypta fumiferanae ichnovirus segment B18, complete sequence',
 'NC_008934.2': 'Glypta fumiferanae ichnovirus segment B18, complete sequence 2',
 'NC_008934.3': 'Glypta fumiferanae ichnovirus segment B18, complete sequence 3',
 'NC_008934.4': 'Glypta fumiferanae ichnovirus segment B18, complete sequence 4'}