Python 如何将列表转换为csv?

Python 如何将列表转换为csv?,python,postgresql,csv,Python,Postgresql,Csv,我有一个来自对postgresql的sql查询的值(我更改了行的颜色以显示firts值是相同的) 我想转换到下一个,转换成csv文件 80255;4;1;2 80425;1;22;5 对于每个sku(如80425),我想将库存放在每个位置(GRN/BCN/PT)当您有一个具有多个值的键时,一个很好的工具是defaultdict容器为每个新键创建一个新对象,当它遇到一个已经存在的键时,它会对该键进行操作;在这种情况下,操作是list方法.append() 然后您可以使用模块写入文件 继续@ber

我有一个来自对postgresql的sql查询的值(我更改了行的颜色以显示firts值是相同的)

我想转换到下一个,转换成csv文件

80255;4;1;2
80425;1;22;5

对于每个sku(如80425),我想将库存放在每个位置(GRN/BCN/PT)

当您有一个具有多个值的键时,一个很好的工具是
defaultdict
容器为每个新键创建一个新对象,当它遇到一个已经存在的键时,它会对该键进行操作;在这种情况下,操作是list方法
.append()

然后您可以使用模块写入文件


继续@bernie的精彩回答,这里有一个带有更多注释的变体,它不依赖于行顺序,可以处理缺少的值:

# import collections and abbreviate it as co
import collections as co
import decimal as dc
import csv

fromdb = [('80255', 'GRN', dc.Decimal('4.00000000000000000000'))
         ,('80425', 'GRN', dc.Decimal('1.00000000000000000000'))
         ,('80255', 'BCN', dc.Decimal('1.00000000000000000000'))
         ,('80425', 'BCN', dc.Decimal('22.0000000000000000'))
         ,('80255', 'PT', dc.Decimal('2.0000000000000000'))
         ,('80425', 'PT', dc.Decimal('5.0000000000000000'))]

# Build a dictionary that maps values like '80255'
# to a dictionary containing values for GRN, BCN and PT
tocsv = co.defaultdict(lambda:
    {'GRN': 0, 'BCN': 0, 'PT': 0} )
for i in fromdb:
    # Array index -1 refers to the last element
    # For example, set tocsv['80255']['GRN'] to 4.00
    tocsv[i[0]][i[1]] = i[-1]

with open('output.txt','wb') as f:
    cw = csv.writer(f,delimiter=';')
    # items() returns an iterable of key (k) and value (v) pairs
    # For key '80255', the value will be {'GRN': 4, 'BCN': 1, 'PT': 2}
    for k,v in tocsv.items():
        # writerow() takes an array argument, hence the [] square brackets
        cw.writerow([k, v['GRN'], v['BCN'], v['PT']])

+1看起来您依赖于
GRN->BCN->PT
行的顺序。问题并不是说你能做到,即使你做到了,也有一些数字可能遗漏了
BCN
行。哦,我明白你的意思了。我的代码中没有对数据库中的结果进行排序的内容,也没有对缺少的值进行检查,这是正确的。
import csv, collections as co, decimal as dc, itertools as it

fromdb = [('80255', 'GRN', dc.Decimal('4.00000000000000000000'))
         ,('80425', 'GRN', dc.Decimal('1.00000000000000000000'))
         ,('80255', 'BCN', dc.Decimal('1.00000000000000000000'))
         ,('80425', 'BCN', dc.Decimal('22.0000000000000000'))
         ,('80255', 'PT', dc.Decimal('2.0000000000000000'))
         ,('80425', 'PT', dc.Decimal('5.0000000000000000'))]

tocsv = co.defaultdict(list)
for i in fromdb:
    tocsv[i[0]].append(i[-1])

with open('output.txt','wb') as f:
    cw = csv.writer(f,delimiter=';')
    for k,v in tocsv.items():
        cw.writerow(tuple(it.chain((k,),v)))
# import collections and abbreviate it as co
import collections as co
import decimal as dc
import csv

fromdb = [('80255', 'GRN', dc.Decimal('4.00000000000000000000'))
         ,('80425', 'GRN', dc.Decimal('1.00000000000000000000'))
         ,('80255', 'BCN', dc.Decimal('1.00000000000000000000'))
         ,('80425', 'BCN', dc.Decimal('22.0000000000000000'))
         ,('80255', 'PT', dc.Decimal('2.0000000000000000'))
         ,('80425', 'PT', dc.Decimal('5.0000000000000000'))]

# Build a dictionary that maps values like '80255'
# to a dictionary containing values for GRN, BCN and PT
tocsv = co.defaultdict(lambda:
    {'GRN': 0, 'BCN': 0, 'PT': 0} )
for i in fromdb:
    # Array index -1 refers to the last element
    # For example, set tocsv['80255']['GRN'] to 4.00
    tocsv[i[0]][i[1]] = i[-1]

with open('output.txt','wb') as f:
    cw = csv.writer(f,delimiter=';')
    # items() returns an iterable of key (k) and value (v) pairs
    # For key '80255', the value will be {'GRN': 4, 'BCN': 1, 'PT': 2}
    for k,v in tocsv.items():
        # writerow() takes an array argument, hence the [] square brackets
        cw.writerow([k, v['GRN'], v['BCN'], v['PT']])