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[10]] += 1
with open('/pythonwork/thefile_subset11.csv', 'w') as outfile:
writer = csv.writer(outfile)
for row in data:
if counter[row[10]] >= 504:
writer.writerow(row)
Run Code Online (Sandbox Code Playgroud)
此代码读取thefile.csv,进行更改并将结果写入thefile_subset1.
但是,当我在Microsoft Excel中打开生成的csv时,每条记录后都会有一个额外的空白行!
有没有办法让它没有多余的空白?
plaintext = input("Please enter the text you want to compress")
filename = input("Please enter the desired filename")
with gzip.open(filename + ".gz", "wb") as outfile:
outfile.write(plaintext)
Run Code Online (Sandbox Code Playgroud)
上面的python代码给出了以下错误:
Traceback (most recent call last):
File "C:/Users/Ankur Gupta/Desktop/Python_works/gzip_work1.py", line 33, in <module>
compress_string()
File "C:/Users/Ankur Gupta/Desktop/Python_works/gzip_work1.py", line 15, in compress_string
outfile.write(plaintext)
File "C:\Python32\lib\gzip.py", line 312, in write
self.crc = zlib.crc32(data, self.crc) & 0xffffffff
TypeError: 'str' does not support the buffer interface
Run Code Online (Sandbox Code Playgroud) 我有一个example.csv包含内容的文件
1,"A towel,",1.0
42," it says, ",2.0
1337,is about the most ,-1
0,massively useful thing ,123
-2,an interstellar hitchhiker can have.,3
Run Code Online (Sandbox Code Playgroud)
我如何example.csv用Python 阅读?
同样,如果我有
data = [(1, "A towel,", 1.0),
(42, " it says, ", 2.0),
(1337, "is about the most ", -1),
(0, "massively useful thing ", 123),
(-2, "an interstellar hitchhiker can have.", 3)]
Run Code Online (Sandbox Code Playgroud)
如何data使用Python 写入CSV文件?
在Windows 8上使用Python 3.3,当写入CSV文件时,我收到错误TypeError: 'str' does not support the buffer interface并使用了"wb"标志.但是当只使用"w"标志时,我没有错误,但是每一行都被一个空行隔开!
码
test_file_object = csv.reader( open("./files/test.csv", 'r') )
next(test_file_object )
with open("./files/forest.csv", 'wb') as myfile:
open_file_object = csv.writer( open("./files/forest.csv", 'wb') )
i = 0
for row in test_file_object:
row.insert(0, output[i].astype(np.uint8))
open_file_object.writerow(row)
i += 1
Run Code Online (Sandbox Code Playgroud)
错误
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-121-8cbb94f602a8> in <module>()
8 for row in test_file_object:
9 row.insert(0, output[i].astype(np.uint8))
---> 10 open_file_object.writerow(row)
11 i += 1
TypeError: 'str' does not …Run Code Online (Sandbox Code Playgroud)