python-删除名称空间并仅打印所需的字符串

时间:2018-10-09 14:46:13

标签: python

我有下面的代码将值存储在列表中。输出将打印列表中的所有值,但带有名称空间。我的问题是,

  1. 如何在没有命名空间的情况下进行打印?
  2. 如何仅打印我需要的值?

例如,从下面的输出中,我只需要打印name2,

['Channel1\nName2\n4Time']

下面是我的代码,

# -*- coding: utf-8 -*-
    import subprocess, sys
    firstarg=sys.argv[1]
    result = []
    service_provider = subprocess.Popen (['ffprobe', '-loglevel', 'fatal', '-select_streams', 'p:program_id=p:1', '-show_entries', 'program_tags=service_name', '-of', 'csv=%s' % ("p=0"), '-i', sys.argv[1]], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    out = service_provider.communicate() [0]
    result = list()
    result.append(out)
    print result

4 个答案:

答案 0 :(得分:0)

[e.split('\n')[1] for e in ['Channel1\nName2\n4Time']]

Out[256]: ['Name2']

答案 1 :(得分:0)

您可以将字符串除以 //create value axes for both temperature and precip values "valueAxes": [{ "id": "temperature", "title": "Temperature (C)" }, { "id": "precipitation", "title": "Precipitation (mm)", "position": "right" }], "synchronizeGrid": true, //make sure the grids from both axes are synchronized "graphs": [{ "bullet": "round", "valueField": "temperature" },{ "fillAlphas": 0.9, "lineAlpha": 0.2, "type": "column", "valueField": "precipitation", "valueAxis": "precipitation" //plot this against the precipitation value axis }], "categoryField": "date", "categoryAxis": { "parseDates": true, "minPeriod": "hh" //make sure we plot hourly data correctly }, 字符并获得所需的字符串

"\n"

输出

x = ['Channel1\nName2\n4Time']
x[0].split("\n")[1]

答案 2 :(得分:0)

您可以通过替换以下代码来简化代码并解决问题:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>

  <style> 
  img {
    border: 1px solid black;
  }
  </style>
</head>

<body>
  <p>
    <img src="photo.jpeg" style="float: left;"/>
    <p>
    <ul>
      <li>This photo is amazing</li>
      <li>Great view!</li>
      <li>But why are the bullet points in the photo??</li>
      <li>Good question, indeed!</li>
    </ul>
  </p>
</body>

</html>

具有:

result = list()
result.append(out)

答案 3 :(得分:0)

您可以使用split()并在'\ n'上拆分字符串

x = ['Channel1\nName2\n4Time', 'Channel1\nName2\n4Time']
[i.split('\n') for i in x]

Output: [['Channel1', 'Name2', '4Time'], ['Channel1', 'Name2', '4Time']]

然后使用列表选择要使用索引的任何元素。