使用 Python 将结果保存到 csv 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3345336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:33:48  来源:igfitidea点击:

Save results to csv file with Python

pythoncsv

提问by l--''''''---------''''''''''''

import csv

with open('test.csv', 'rb') as f:
  data = list(csv.reader(f))

import collections
counter = collections.defaultdict(int)
for row in data:
    counter[row[1]] += 1
for row in data:
    if counter[row[1]] >= 4:
      writer = csv.writer(open("test1.csv", "wb"))
      writer.writerows(row)

I am getting strange output! What is wrong with this code?

我得到奇怪的输出!这段代码有什么问题?

采纳答案by danben

Use csv.writer:

使用csv.writer

import csv

with open('thefile.csv', 'rb') as f:
  data = list(csv.reader(f))

import collections
counter = collections.defaultdict(int)
for row in data:
    counter[row[0]] += 1


writer = csv.writer(open("/path/to/my/csv/file", 'w'))
for row in data:
    if counter[row[0]] >= 4:
        writer.writerow(row)

回答by l--''''''---------''''''''''''

An easy example would be something like:

一个简单的例子是这样的:

writer = csv.writer(open("filename.csv", "wb"))
String[] entries = "first#second#third".split("#");
writer.writerows(entries)
writer.close()

回答by Waqas

You can close files not csv.writer object, it should be:

您可以关闭文件而不是 csv.writer 对象,它应该是:

f = open(fileName, "wb")
writer = csv.writer(f)
String[] entries = "first*second*third".split("*");
writer.writerows(entries)
f.close()

回答by Joseph Hansen

I know the question is asking about your "csv" package implementation, but for your information, there are options that are much simpler — numpy, for instance.

我知道问题是询问您的“csv”包实现,但对于您的信息,有一些更简单的选项 - 例如 numpy。

import numpy as np
np.savetxt('data.csv', (col1_array, col2_array, col3_array), delimiter=',')


(This answer posted 6 years later, for posterity's sake.)

(为了后代,这个答案在 6 年后发布。)

In a different case similar to what you're asking about, say you have two columns like this:

在与您所询问的类似的不同情况下,假设您有两列这样的:

names = ['Player Name', 'Foo', 'Bar']
scores = ['Score', 250, 500]

You could save it like this:

你可以这样保存:

np.savetxt('scores.csv', [p for p in zip(names, scores)], delimiter=',', fmt='%s')

scores.csvwould look like this:

scores.csv看起来像这样:

Player Name,Score
Foo,250
Bar,500

回答by Python lover

This is how I do it

这就是我的做法

 import csv
    file = open('???.csv', 'r')
    read = csv.reader(file)
    for column in read:
            file = open('???.csv', 'r')
            read = csv.reader(file)
            file.close()
            file = open('????.csv', 'a', newline='')
            write = csv.writer(file, delimiter = ",")
            write.writerow((, ))
            file.close()

回答by kiran

this will provide exact output

这将提供准确的输出

import csv

import collections

with open('file.csv', 'rb') as f:

  data = list(csv.reader(f))

counter = collections.defaultdict(int)

for row in data:

    counter[row[0]] += 1

writer = csv.writer(open("file1.csv", 'w'))

for row in data:

    if counter[row[0]] >= 1:

        writer.writerow(row)