换行符不适用于 python 2.7 [英] newline does not work with python 2.7

查看:48
本文介绍了换行符不适用于 python 2.7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我写了一个 python 脚本来格式化文本文件,以便我可以导入到我的 SQL 中.我正在使用 python 3.5,我的代码运行良好.

So I wrote a python script that formats a text file so I can import into my SQL. I am using python 3.5 and my code works perfectly.

但是,当我尝试在 python 2.7 中运行我的代码时,它不起作用并抛出此错误.(我必须使用 2.7)直到后来我才知道这一点.

However, When I try to run my code in python 2.7, it does not work and it throws this error. ( I have to use 2.7) I did not know this till after.

TypeError: 'newline' is an invalid keyword argument for this function.

有没有办法解决这个问题,如果我不使用换行符,它会跳过我的数据中的行并显示为一个空行.

Is there a way around this, if I don't use newline, it skips rows in my data and it shows as a blank row.

这是我的代码:

import csv
import os


my_file_name = os.path.abspath('NVG.txt')
cleaned_file = "cleanNVG.csv"
BulkImport_file = 'BulkImport.txt'
remove_words = ['INAC-EIM','-INAC','TO-INAC','TO_INAC','SHIP_TO-inac','SHIP_TOINAC']


with open(my_file_name, 'r', newline='') as infile, open(cleaned_file, 'w',newline='') as outfile:
    writer = csv.writer(outfile)
    cr =  csv.reader(infile, delimiter='|')
    writer.writerow(next(cr)[:25])
    for line in (r[0:25] for r in cr):

        if not any(remove_word in element for element in line for remove_word in remove_words):
         line[11]= line[11][:5]

         writer.writerow(line)
infile.close()
outfile.close()

with open(cleaned_file, 'r') as fin, open(BulkImport_file, 'w') as fout:
        reader = csv.DictReader(fin)
        writer = csv.DictWriter(fout, reader.fieldnames, delimiter='|')
        writer.writeheader()
        writer.writerows(reader)

如何修改我的代码以使其与 python 2.7 兼容.非常感谢!

How can I modify my code so it's compatible with python 2.7. Thanks so much!

推荐答案

简短的回答:使用 io.open,它与 python 3 的 open 具有相同的签名.

The short answer: use io.open which has the same signature as python 3's open.

csv 模块为您处理行尾,以便它可以处理不同于本地文件系统编码的行尾.例如,即使在 linux 上,方言也可能想写 \r\n 行结尾.在python 2中,解决方案是以二进制模式打开文件.

The csv module handles line ending for you so that it can deal with line endings different than local file system encodings. For instance, a dialect may want to write \r\n line endings even on linux. In python 2, the solution was to open files in binary mode.

在 python 3 中,情况有所不同.以二进制模式打开的文件返回 bytes 对象,这些对象需要解码才能成为 unicode 字符串对象.您可以在文本模式下打开,但这有两件事 - 解码和换行翻译.于是发明了 newline 关键字.它允许您在文本模式下打开以进行解码,但在字符串中保留换行符.

In python 3, things are different. Files opened in binary mode return bytes objects that need decoding to become unicode string objects. You can open in text mode, but that does two things - decoding and newline translation. So the newline keyword was invented. It lets you open in text mode for decoding but leaves the newline terminator in the string.

此功能在 python 2 和 python 3 上的 io.open 函数中也可用.您可以使用该函数来获得您想要的.请注意,您还需要做出某种解码决定.默认情况下,无论 sys.getfilesystemencoding() 返回什么.您可能需要首先决定如何对 csv 文件进行编码,然后在文件中使用该编码.

This functionality is also available in the io.open function available on both python 2 and 3. You can use that function to get what you want. Notice that you also need to make some sort of a decoding decision. By default its whatever sys.getfilesystemencoding() returns. You may need to make a decision about how you are encoding your csv files in the first place and use that encoding in your files.

这篇关于换行符不适用于 python 2.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆