相关疑难解决方法(0)

用Python编写的CSV文件在每行之间都有空行

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时,每条记录后都会有一个额外的空白行!

有没有办法让它没有多余的空白?

python windows csv

394
推荐指数
7
解决办法
24万
查看次数

TypeError:'str'不支持缓冲区接口

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)

python string gzip

263
推荐指数
5
解决办法
33万
查看次数

如何使用Python读写CSV文件?

我有一个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文件?

python csv

33
推荐指数
1
解决办法
4万
查看次数

Python 3.3 CSV.Writer写入额外的空白行

在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)

python csv python-3.x

19
推荐指数
2
解决办法
2万
查看次数

标签 统计

python ×4

csv ×3

gzip ×1

python-3.x ×1

string ×1

windows ×1