python - CSV 阅读器在 Ubuntu 和 Windows 上的不同输出 - Python

标签 python linux windows python-2.7 csv

<分区>

我有一个名为 Some.csv 的文件,它有一个字段(列),其值类似于

1000, 2000, .... 39000.

我只想要 39000 的文件,所以我编写了以下 python 脚本

import os
import csv

with open('Somenew.csv', 'w') as fw:
  writr = csv.writer(fw, delimiter=',')
  with open('Some.csv','r') as fr:
    reader = csv.reader(fr, delimiter = ',')
    for row in reader:
      if row[2] == '39000':  writr.writerow(row)

现在当它在 Windows 上执行时,它的 Somenew.csv 看起来像这样

xxxx, xxxx, 39000, xxxx, xxxx

yyyy, yyyy, 39000, yyyy, yyyy

但是在 Linux (Ubuntu 14.04LTS) 上执行时没有添加额外的换行符。

这有什么原因吗?可能是编译器的问题?

最佳答案

如果您使用的是 Python 2.x,则必须对 csv 文件使用 wb 模式:

https://docs.python.org/2/library/csv.html#csv.writer

If csvfile is a file object, it must be opened with the ‘b’ flag on platforms where that makes a difference.

关于python - CSV 阅读器在 Ubuntu 和 Windows 上的不同输出 - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36494688/

相关文章:

python - Otsu 阈值 OpenCV Python

python - 使用 CSV 文件的 Matplotlib 多线图

c++ - .pdb 中的函数地址与.exe 不同,为什么?

windows - 每分钟在后台运行一次 powershell 脚本

Python如何将混合列表写入csv文件?

python - 使用python将特殊值粘贴到另一个多个excel文件

linux - 使用参数运行 Linux shell 脚本

linux - 汇编程序段失败

linux - 我怎样才能显示正确答案?

windows - 来自CMD命令提示符的Docker错误:运行SSH命令时出现问题