在 python 中输入多列/行原始输入后连接列表的值
Concatenate the values of a list after inputting multiple column / row raw input in python
我正在尝试使用此处的程序将复制和粘贴的列表从 excel 转换为我可以在 Python 中使用连接操作的列表。当我输入多个列/行值时,我得到一个我在操作时遇到问题的列表。请参阅以下示例:
- 程序如下:
def detail_input():
try:
while True:
data=raw_input()
if not data: break
yield data
except KeyboardInterrupt:
return
- 原始输入从 excel 复制到 unix 命令行:
123 56
321 42
987 24
- 然后我得到一个 return,看起来像这样:
userInput = list(detail_input())
[['123\t56'],['321\t42'],['987\t24']]
- 我想要的输出如下所示:
该组是 123,他们的个人资料是 56
该组是 321,他们的个人资料是 42
该组是 987,他们的个人资料是 24
我尝试使用以下方法删除制表符分隔符:
values = list(csv.reader(userInput, delimiter='\t'))
- 但它将列表转换为元组,我无法提取单个值 - 我只能提取每个括号内的两个值:\
[['123','56'],['321','42'],['987','24']]
有什么想法请帮忙
你可以做到这一点。
values = list(csv.reader(userInput, delimiter='\t'))
# add the following line to print what you need.
[print("the group is {} and their profile is {}".format(each[0], each[1])) for each in values]
或者像下面这样简单的东西。
values = list(csv.reader(userInput, delimiter='\t'))
for each in values:
group = each[0]
profile = each[1]
print("the group is {} and their profile is {}".format(group,profile))
如果格式稳定(总是像 \d+\t\d+
这样的数字,然后是制表符,然后是另一个数字),您可以这样做
values = {sub_list[0].split('\t')[0]: sub_list[0].split('\t')[1] for sub_list in userInput}
获取字典
然后像
for key, value in values.items():
print(f'the group is {key} and their profile is {value}')
但这种方法不是一个好的开始,您最好从 excel 导出 csv 或 tsv 文件并使用 csv 库读取数据。
我正在尝试使用此处的程序将复制和粘贴的列表从 excel 转换为我可以在 Python 中使用连接操作的列表。当我输入多个列/行值时,我得到一个我在操作时遇到问题的列表。请参阅以下示例:
- 程序如下:
def detail_input():
try:
while True:
data=raw_input()
if not data: break
yield data
except KeyboardInterrupt:
return
- 原始输入从 excel 复制到 unix 命令行:
123 56
321 42
987 24
- 然后我得到一个 return,看起来像这样:
userInput = list(detail_input())
[['123\t56'],['321\t42'],['987\t24']]
- 我想要的输出如下所示:
该组是 123,他们的个人资料是 56
该组是 321,他们的个人资料是 42
该组是 987,他们的个人资料是 24
我尝试使用以下方法删除制表符分隔符:
values = list(csv.reader(userInput, delimiter='\t'))
- 但它将列表转换为元组,我无法提取单个值 - 我只能提取每个括号内的两个值:\
[['123','56'],['321','42'],['987','24']]
有什么想法请帮忙
你可以做到这一点。
values = list(csv.reader(userInput, delimiter='\t'))
# add the following line to print what you need.
[print("the group is {} and their profile is {}".format(each[0], each[1])) for each in values]
或者像下面这样简单的东西。
values = list(csv.reader(userInput, delimiter='\t'))
for each in values:
group = each[0]
profile = each[1]
print("the group is {} and their profile is {}".format(group,profile))
如果格式稳定(总是像 \d+\t\d+
这样的数字,然后是制表符,然后是另一个数字),您可以这样做
values = {sub_list[0].split('\t')[0]: sub_list[0].split('\t')[1] for sub_list in userInput}
获取字典
然后像
for key, value in values.items():
print(f'the group is {key} and their profile is {value}')
但这种方法不是一个好的开始,您最好从 excel 导出 csv 或 tsv 文件并使用 csv 库读取数据。