简体   繁体   English

在python中的2列CSV文件中写入2个列表

[英]Write 2 lists in 2 columns of CSV file in python

I have 2 lists let's say我有 2 个列表让我们说

a = [1,2,3] 
b = [4,5,6]

I want to write them in two columns in CSV file so when I open the excel sheet I see something like this :我想将它们写在 CSV 文件的两列中,所以当我打开 Excel 表格时,我看到如下内容:

col1              col2

1                  4

2                  5

3                  6

How can I do this?我怎样才能做到这一点?

I used zip(a,b) but the result is stored in one column:我使用了zip(a,b)但结果存储在一列中:

col1 

1 4

2 5

3 6

You need to use csv.Dictwriter() to be able to specify the field names.您需要使用csv.Dictwriter()才能指定字段名称。

import csv

with open('numbers.csv', 'w', newline='') as csvfile:
    fieldnames = ['col1', 'col2']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()
    for i, j in zip(a, b):
        writer.writerow({'col1': i, 'col2': j})

Or instead of a regular for loop you could use writerows and a list comprehension:或者,您可以使用writerows和列表理解来代替常规的for循环:

writer.writerows([{'col1': i, 'col2': j} for i,j in zip(a,b)])

Using pandas is very easy.使用pandas非常简单。 Just:只是:

import pandas as pd

and then:接着:

In [13]: df = pd.DataFrame({'col1':a, 'col2':b})

In [14]: df
Out[14]: 
   col1  col2
0     1     4
1     2     5
2     3     6

In [15]: df.to_csv('numbers.csv', index=False)

Basically you are building a dataframe with your lists and then save back to .csv .基本上,您正在使用列表构建数据框,然后保存回.csv Hope that helps.希望有帮助。

9963354538:8790348643 9963354539:8790348644 9963354530:8790348645 9963354531:8790348646 9963354532:8790348647 9963354533:8790348648 9963354534:8790348649 9963354535:8790348640 9963354536:8790348641 9963354537:8790348642 9963354538:8790348643 9963354539:8790348644 9963354530:8790348645 9963354531:8790348646 9963354532:8790348647 9963354533:8790348648 9963354534:8790348649 9963354535:8790348640 9963354536:8790348641 9963354537:8790348642

this is the csv file i have and i want to convert this file into这是我拥有的 csv 文件,我想将此文件转换为

[{'batch': '9963354538', 'pin': '8790348643'}, {'batch': '9963354539', 'pin': '8790348644'}, {'batch': '9963354530', 'pin': '8790348645'}, {'batch': '9963354531', 'pin': '8790348646'}, {'batch': '9963354532', 'pin': '8790348647'}, {'batch': '9963354533', 'pin': '8790348648'}, {'batch': '9963354534', 'pin': '8790348649'}, {'batch': '9963354535', 'pin': '8790348640'}, {'batch': '9963354536', 'pin': '8790348641'}, {'batch': '9963354537', 'pin': '8790348642'}] [{'batch':'9963354538','pin':'8790348643'},{'batch':'9963354539','pin':'8790348644'},{'batch':'9963354530': '8790348645'},{'batch':'9963354531','pin':'8790348646'},{'batch':'9963354532','pin':'8790348647'},'9563'},'9563' 'pin':'8790348648'},{'batch':'9963354534','pin':'8790348649'},{'batch':'9963354535','pin':'8790348640'} '9963354536','pin':'8790348641'},{'batch':'9963354537','pin':'8790348642'}]

this form list of dictionaries这种形式的字典列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM