【问题标题】:create a list where each line has multiple fields in python创建一个列表,其中每行在 python 中有多个字段
【发布时间】:2019-09-12 16:49:55
【问题描述】:

我有一个将其输出到标准输出的脚本。

FIeld1 field2 = field3

大约有 30 个这样的文件 我不想使用 print 打印它。将它分配给一个列表

但我不知道如何..

我有这样的事情:

data = []
field2 = 'toto'
field1 = 'foo'

for A in some_dict:
   print(field1, field2, "=", " ".join(str(x) for x in A.values()))

我尝试过数据追加和 data.extend 总是出错

for A in aliases:
   data.append(field1 + field2 + "=" + " ".join(str(x) for x in A.values()))

但值内没有空格..

【问题讨论】:

  • 你能分享你得到的错误
  • 问题是没有空格,如果我手动添加空格,我会得到data.append(uid + host + "=" + " ".join(str(x) for x in A.values() + '\n')) TypeError: can only concatenate list (not "str") to list
  • 如果正确运行,数据会是什么样子?

标签: python python-2.7 list format


【解决方案1】:

如果我理解你想要做的事情是这样的:

for A in aliases:
    data.append(field1 + " " + field2 + " = " + " ".join(str(x) for x in A.values()))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    相关资源
    最近更新 更多