[英]Python: Writing Counter to a csv file
I have a csv file of data that has the columns 'number'
, 'colour'
, 'number2'
, 'foo'
, 'bar'
, which looks like: 我有一个csv数据文件,其中包含
'number'
, 'colour'
, 'number2'
, 'foo'
, 'bar'
,如下所示:
12, red, 124, a, 15p
14, blue, 353, c, 7g
12, blue, 125, d, 65h
12, red, 124, c, 12d
I want to count the number of times number, colour and number2 occur together, so for example, the output from the above list would be: '12, red, 124 :2','14, blue, 353: 1', '12, blue, 125: 1'
. 我想计算数字,颜色和数字2一起出现的次数,例如,上面列表的输出将是:
'12, red, 124 :2','14, blue, 353: 1', '12, blue, 125: 1'
。 I've done this by using: 我通过使用:
import csv
datafile=open('myfile.csv','r')
usefuldata=[]
for line in datafile:
usefuldata.append(line)
from collections import Counter
outfile1=Counter((line[1],line[2],line[3]) for line in usefuldata)
print(outfile1)
This gives me : 这给了我:
Counter({(‘12’,’red’,’135’): 21, (‘15’,’blue’,’152’):18, (‘34’,’green’,’123’):16 etc})
Which is great, but I'd like to write this out to a file. 哪个好,但我想把它写到一个文件中。 I'd like the file to have 4 columns: number, colour, number2, and count.
我希望文件有4列:数字,颜色,数字2和计数。 I realise this is a common question and I've tried a few different approaches suggested on other threads, but none have worked.
我意识到这是一个常见问题,我尝试过在其他线程上提出的一些不同的方法,但没有一个有效。
Newfile=open(‘newfile.csv’,’wb’)
fieldnames=['a','b']
csvwriter=csv.DictWriter(newfile, delimiter=',', fieldnames=fieldnames)
csvwriter.writerow(dict((fn,fn) for fn in fieldnames))
for row in outfile1:
csvwriter.writerow(row)
And 和
with open('newfile.csv','wb') as csvfile:
fieldnames=['number','colour','number2']
writer=csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow(Counter((line[1],line[2],line[3]) for line in usefuldata))
countwriter=csv.writer(csvfile, delimiter=', ')
countwriter.writerow(outfile1)
Both give me the error 两者都给我错误
return self.writer.writerow(self._dict_to_list(rowdict))
TypeError: 'str' does not support the buffer interface
I've also tried using pickle: 我也尝试过使用泡菜:
import pickle
with open('newfile.csv','wb') as outputfile:
pickle.dump(outfile1, outputfile)
gives me gibberish files. 给了我乱码文件。
My current attempt is to use 我目前的尝试是使用
writer=csv.DictWriter(newfile, outfile1)
for line in outfile1:
writer.writerow(line)
but this gives me an error about fieldnames. 但是这给了我关于字段名的错误。
I know this is a common question and I'm conscious that I'm only struggling because I really don't know what I'm doing- it has been a few years since I've used python and I've forgotten so much. 我知道这是一个常见的问题,我知道我只是在挣扎,因为我真的不知道自己在做什么 - 自从我使用python已经过去几年了,我已经忘记了这么多。 Any help would be greatly appreciated.
任何帮助将不胜感激。
First of all, the reason for the main issue - 首先,主要问题的原因 -
TypeError: 'str' does not support the buffer interface
is that you are openning the file in binary mode, you should open the file in text mode ( without b
). 是你在二进制模式下打开文件,你应该以文本模式打开文件(没有
b
)。
Secondly, I would say it would be easier to use normal csv.writer
than csv.DictWriter()
in your case, because of the way your dictionary is created. 其次,我会说在你的情况下使用普通的
csv.writer
比使用csv.DictWriter()
更容易,因为你的字典的创建方式。
A way to write your result to csv would be - 将结果写入csv的方法是 -
#Assuming you have previously created the counter you want to write
#lets say you stored the counter in a variable called cnter
with open('newfile.csv','w') as csvfile:
fieldnames=['number','colour','number2','count']
writer=csv.writer(csvfile)
writer.writerow(fieldnames)
for key, value in cnter.items():
writer.writerow(list(key) + [value])
for me the above solution did not work. 对我来说,上述解决方案无效。 It splitted all the characters of the word in separate columns so the output was "every character in a separate column followed by the count" rather than entire word in one column followed by count.
它在单独的列中分割了单词的所有字符,因此输出是“单独列中的每个字符后跟计数”,而不是一列中的整个单词,后跟count。 It might have been due to some other errors that i might have made.
这可能是由于我可能做出的其他一些错误。 However for me the below code worked ::
但对我来说下面的代码工作::
with open(outfile, encoding='utf-8-sig', mode='w') as fp:
fp.write('KMC,freq\n')
for tag, count in cnt.items():
fp.write('{},{}\n'.format(tag, count))
I hope this is helpful for others 我希望这对其他人有帮助
import csv
Assuming count is a Python 3 Counter. 假设count是一个Python 3计数器。
If key is a string, to not split it in every character it contains : 如果key是一个字符串,则不在它包含的每个字符中将其拆分:
with open(root+'counter_test.csv','w') as csvfile:
writer=csv.writer(csvfile)
for key, value in count.items():
writer.writerow([key] + [value])
And even simpler (take care of the 's' to writerows() function) : 甚至更简单(照顾's'到writerows()函数):
with open(root+'counter_test.csv','w') as csvfile:
writer=csv.writer(csvfile)
writer.writerows(count.items())
Simply by using for loop we can add sequence counter to the CSV reade following code will desplay the counter to the csv file 只需使用for循环,我们就可以在CSV reade中添加序列计数器,代码将显示csv文件的计数器
import csv
x=0
reader = csv.reader(open("c:/path/abc.csv"))
for raw in reader:
x += 1
print(raw)
print(x)
above code will desplay output like this 上面的代码会像这样显示输出
['id', 'fname', 'lname'] 1 ['1', 'a', 'x'] 2 ['2', 'b', 'y'] 3 ['3', 'c', 'z'] 4 ['id','fname','lname'] 1 ['1','a','x'] 2 ['2','b','y'] 3 ['3','c' ,'z'] 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.