【问题标题】:Header not getting written in csv at the time of export from python从 python 导出时,标头没有写入 csv
【发布时间】:2015-04-29 13:03:35
【问题描述】:

我正在使用 python 3.4。我正在将一些结果从 python 导出到 csv 文件。但是我的标题没有被写入。下面给出的是我正在使用的一段代码

with open('C:/Twitter_crawl/scoring_tweet.csv', 'r') as fp:

with open('scored_tweets.csv', 'w',newline='') as op:
    headers=['processed_tweet','sentiment']
    a = csv.DictWriter(op,delimiter=',',fieldnames=headers)
    for line in fp.readlines():
        sentiment = NBClassifier.classify(extract_features(getFeatureVector(line)))
        a.writerow({'processed_tweet':line,'sentiment':sentiment})

创建该文件时填充了数据但没有标题。谁能帮帮我?

提前致谢!!

【问题讨论】:

    标签: python-3.x export-to-csv


    【解决方案1】:

    要写入表头,必须调用 csv.DictWriter.writeheader 方法:writerow 只写行...

    【讨论】:

      猜你喜欢
      • 2015-12-08
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多