从subprocess.Popen输出打印特定行

时间:2017-09-02 16:39:17

标签: python-3.x

我正在使用Shairport-Sync元阅读器管道播放数据,并且我正在使用以下python3代码返回字符串:

#!/usr/bin/python
import subprocess
cmd = "shairport-sync-metadata-reader < /tmp/shairport-sync-metadata"
proc = subprocess.Popen(cmd,stdout=subprocess.PIPE,shell=True)

while True:
  line = proc.stdout.readline()
  print (line)

#   if line != '':
#      wordKeys = ["Artist", "Title", "Album"]
#     lineNow = line.rstrip()
#       for key in wordKeys:
#         if key in lineNow:
#          print (lineNow)
#    else:
#      break

我无法使用网络上找到的代码隔离艺术家,标题和专辑字符串。我在最后一行注释时遇到的错误是指'type str不支持缓冲区API'。

我的proc.stdout.readline()输出如下:

b'Client\'s IP: "fe80::1477:d5d0:763a:a093".\n'
b'"ssnc" "svip": "fe80::25f8:b47e:c427:e431".\n'
b'"ssnc" "snua": "AirPlay/352.17.1".\n'
b'"ssnc" "acre": "2761427076".\n'
b'"ssnc" "daid": "FA4CC8448AD2A90E".\n'
b'"ssnc" "pbeg": "".\n'
b'"ssnc" "pvol": "-11.74,-19.48,-96.30,0.00".\n'
b'"ssnc" "pfls": "".\n'
b'"ssnc" "pcst": "2456171464".\n'
b'Picture received, length 95419 bytes.\n'
b'"ssnc" "pcen": "2456171464".\n'
b'"ssnc" "prgr": "2455930277/2456184560/2468648320".\n'
b'"ssnc" "mdst": "2456186320".\n'
b'Album Name: "Funeral".\n'
b'Artist: "Arcade Fire".\n'
b'Composer: "Arcade Fire/Josh Deu".\n'
b'Genre: "Indie Rock".\n'
b'Title: "Neighborhood #1 (Tunnels)".\n'
b'"ssnc" "mden": "2456186320".\n'
b'"ssnc" "pcst": "2456186320".\n'
b'Picture received, length 95419 bytes.\n'
b'"ssnc" "pcen": "2456186320".\n'
b'"ssnc" "prgr": "2456019183/2456191600/2468737226".\n'
b'"ssnc" "prsm": "".\n'

我的问题是如何将打印输出减少到与艺术家,曲目和专辑相关的行的值?

1 个答案:

答案 0 :(得分:0)

这些是字节而不是字符串,请参阅例如https://docs.python.org/3/reference/lexical_analysis.html#literals

  

字节文字总是以&#39; b&#39;为前缀。或者&#39; B&#39 ;;他们生产了一个   字节类型的实例而不是str类型。他们可能只是   包含ASCII字符;数字值为128或更大的字节   必须用逃避来表达。

您可以使用>>>import requests >>>print(requests.head('https://knoema.com/BISDPPLS2016/bis-property-prices-long-series')) <Response [200]> >>> print(requests.head('https://beta.knoema.org/BISDPPLS2016/bis-property-prices-long-series')) Connection to beta.knoema.org timed out. Tried below as well, but same error >>>print(requests.head('https://beta.knoema.org/IRFCL2016Jul', timeout=5)) 将其转换为字符串:

decode