Python 如何获得以下格式的列表?

Python 如何获得以下格式的列表?,python,list,Python,List,我在一个文本文件中有以下数据 22882367/pgc orc蜂箱输出 13454914/pqs 2254110952/加工nrt出口 等 需要输出如下格式的数组列表 ——[22882367'/,[pgc-orc-hive-output'],[13454914'/,[pqs'],[2254110952'/,[processed-nrt-export'] 使用下面的strip和split来获得所需的分隔值 file = open('file.txt') my_arr = [] for line

我在一个文本文件中有以下数据

22882367/pgc orc蜂箱输出
13454914/pqs
2254110952/加工nrt出口
等
需要输出如下格式的数组列表

——[22882367'/,[pgc-orc-hive-output'],[13454914'/,[pqs'],[2254110952'/,[processed-nrt-export']

使用下面的
strip
split
来获得所需的分隔值

file = open('file.txt')
my_arr = []
for line in file:
    fields = line.strip().split()
    my_arr.append([fields[0], "/", fields[1][1:]])

print(my_arr)
输出:

[['22882367', '/', 'pgc-orc-hive-output'], ['13454914', '/', 'pqs'], ['2254110952', '/', 'processed-nrt-export']]

我希望这是你所期待的

import re
fname= 'abcd.txt'

lists = []
with open(fname) as f:
    content = f.readlines()

for i in content:
    lists.append(re.split('(/)',i.strip()))


print lists

到目前为止您尝试了什么?您需要从txt读取吗。文件数据是如何组织在文件中的?tap delimited?堆栈溢出不是编码服务。你不能粘贴你的问题,让其他用户帮你解决。请阅读帮助页以了解如何编写一个好问题。@Antwane:虽然理论上你说的是对的,但实际上它一直都在这里发生……使用
.split(“/”
my_arr.append([fields[0],“/”,fields[1]])
可能更容易阅读。谢谢,我不知道如何追加。我尝试了以下代码。将open('/tmp/hdfs_dir','r')作为tsv:line=[elem.strip().split('\t')作为tsv中的elem]不客气;)按预期获得输出,thnks