Python - 将字段导出为CSV

时间:2016-05-02 22:21:44

标签: python csv

for i in tweets_df.ix[:,0]:
    if regex_getter(i) != None:
        city,state = regex_getter(i).split(',')
        print(city,state)

我基本上只是想把城市和州写成csv。打印城市和州打印正好我下面的内容减去标题

我的csv应该看起来像

City State
Burlington  VT
Minneapolis  MN
Bloomington  IN
Irvine  CA

1 个答案:

答案 0 :(得分:1)

您可以使用csv模块执行此操作:

writerow将采用迭代,并在单独的列中编写iterable中的每个项目。

import csv
....
with open("test.csv", "w", newline='') as csvfile:
     writer = csv.writer(csvfile)
     #write header, or something
     for i in tweets_df.ix[:,0]:
          if regex_getter(i) != None:
              data = regex_getter(i).split(',')
              writer.writerow(data)