我正在使用下面的代码在我的freeview框中输入带有我的频道名称的频道号,但由于编码没有列,所以它看起来像这样:
| 101 bbc one
| 102 bbc two
| 103 itv
| 104 channel 4
频道名称的开头由频道号码的宽度决定。我如何编辑我的代码,以便我的所有频道排列如下:
| 101 bbc one
| 102 bbc two
| 103 itv
| 104 channel 4
这是我的代码:
for number in customised["video"].keys():
customised["video"][number]["number"] = number
# swap channel numbers. Only swaps number. Channel swap is still done in bouquetwriter.py
if
providerConfig.isSwapChannels() and "swapchannels" in providers[section_identifier] and len(providers[section_identifier]["swapchannels"]) > 0 and "preferred_order" in providers[section_identifier]["swapchannels"][0]:
for swaprule in providers[section_identifier]["swapchannels"][0]["preferred_order"]:
if
swaprule[0] in services[section_identifier]["video"] and swaprule[1] in services[section_identifier]["video"] and services[section_identifier]["video"][swaprule[1]]["service_type"] >= 17:
customised["video"][swaprule[0]]["number"] = swaprule[1]
customised["video"][swaprule[1]]["number"] = swaprule[0]
for service in sorted(customised["video"].keys()):
if
service in range(1,2000):
if
"interactive_name" in customised["video"][service]:
customised["video"][service]["interactive_name"] = " " * 5 + str(customised["video"][service]["number"]) + " " * 4 + customised["video"][service]["interactive_name"]
else:
customised["video"][service]["interactive_name"] = " " * 5 + str(customised["video"][service]["number"]) + " " * 4 + customised["video"][service]["service_name"]
答案 0 :(得分:1)
目前还不清楚您的设置是什么,但假设第一个频道列表位于名为' channels.txt'的文件中,这样的内容可能有所帮助:
with open('channels.txt', 'r') as fh:
channels = dict([ line.strip().split(None, 1) for line in fh.readlines() ])
channels
现在看起来像这样:
{'102': 'bbc two', '103': 'itv', '101': 'bbc one', '104': ' channel 4'}
我无法告诉您如何构建customized
,但这似乎是您想要的一部分。
答案 1 :(得分:0)
现在你的问题与以前不同了
同样,假设你想要的是一个名为channels.txt
的文件,下面的代码会将它重写为你想要的格式:
with open('channels.txt', 'r') as fh:
channels = '\n'.join([ '{} {:>5} {}'.format(*line.strip().split(None, 2))
for line in fh.read().split('\n') ])
with open('channels.txt', 'w') as fh:
fh.write(channels)