Python列表扩展,用新行追加每个扩展值
请帮助,我正在使用扩展列表将多个值附加到列表中 我需要扩展到列表,作为每次扩展的新行Python列表扩展,用新行追加每个扩展值,python,python-2.7,csv,Python,Python 2.7,Csv,请帮助,我正在使用扩展列表将多个值附加到列表中 我需要扩展到列表,作为每次扩展的新行 >>> list1 = [] >>> list1 = (['Para','op','qa', 'reason']) >>> list1.extend(['Power','pass','ok', 'NA']) >>> print list1 ['Para', 'op', 'qa', 'reason', 'Power', 'pass', 'ok
>>> list1 = []
>>> list1 = (['Para','op','qa', 'reason'])
>>> list1.extend(['Power','pass','ok', 'NA'])
>>> print list1
['Para', 'op', 'qa', 'reason', 'Power', 'pass', 'ok', 'NA']
我需要向csv提供此列表,它必须打印两行
Para, op, qa, reason
Power, pass, ok, NA
如果您想要单独的列表,请将它们分开。不要使用
list.extend()
,使用追加:
现在list1
是一个包含两个对象的列表,每个对象本身就是一个列表:
>>> list1
[['Para', 'op', 'qa', 'reason'], ['Power', 'pass', 'ok', 'NA']]
如果要使用写入CSV文件,请使用将每行写入单独的行:
>>> import csv
>>> import sys
>>> writer = csv.writer(sys.stdout)
>>> writer.writerows(list1)
Para,op,qa,reason
Power,pass,ok,NA
如果您想要单独的列表,请将它们分开。不要使用
list.extend()
,使用追加:
现在list1
是一个包含两个对象的列表,每个对象本身就是一个列表:
>>> list1
[['Para', 'op', 'qa', 'reason'], ['Power', 'pass', 'ok', 'NA']]
如果要使用写入CSV文件,请使用将每行写入单独的行:
>>> import csv
>>> import sys
>>> writer = csv.writer(sys.stdout)
>>> writer.writerows(list1)
Para,op,qa,reason
Power,pass,ok,NA
您想要的结果list1应该是一个包含两个元素的列表,每个元素本身就是一个列表
list1 = ['Para','op','qa', 'reason']
# wrapping list1 with [] crates a new list which its first element is the original list1.
# In your case, this action gives a list of lines with only one single line
# Only after that I can add a new list of lines that contains another single line
list1 = [list1] + [['Power','pass','ok', 'NA']]
print (list1)
您想要的结果list1应该是一个包含两个元素的列表,每个元素本身就是一个列表
list1 = ['Para','op','qa', 'reason']
# wrapping list1 with [] crates a new list which its first element is the original list1.
# In your case, this action gives a list of lines with only one single line
# Only after that I can add a new list of lines that contains another single line
list1 = [list1] + [['Power','pass','ok', 'NA']]
print (list1)