什么是' rb'和' rU'在csv的open()函数中

时间:2016-06-29 07:44:06

标签: python python-3.x csv

我想知道它们之间有什么区别:

with codecs.open('xxxx.csv', 'rU') as h:

with codecs.open('xxxx.csv', 'rb') as h:

我想我记得有人说你应该使用' rb '而不是' rU '当我开始阅读.csv文件的项目时,我似乎无法再找到它。

有人想解释一下吗? 感谢

1 个答案:

答案 0 :(得分:8)

作为the documentation状态,不推荐使用 /** * Since every observable into the zip is created to subscribeOn a diferent thread, it´s means all of them will run in parallel. * By default Rx is not async, only if you explicitly use subscribeOn. */ @Test public void testAsyncZip() { scheduler = Schedulers.newThread(); scheduler1 = Schedulers.newThread(); scheduler2 = Schedulers.newThread(); long start = System.currentTimeMillis(); Observable.zip(obAsyncString(), obAsyncString1(), obAsyncString2(), (s, s2, s3) -> s.concat(s2) .concat(s3)) .subscribe(result -> showResult("Async in:", start, result)); } /** * In this example the the three observables will be emitted sequentially and the three items will be passed to the pipeline */ @Test public void testZip() { long start = System.currentTimeMillis(); Observable.zip(obString(), obString1(), obString2(), (s, s2, s3) -> s.concat(s2) .concat(s3)) .subscribe(result -> showResult("Sync in:", start, result)); } public void showResult(String transactionType, long start, String result) { System.out.println(result + " " + transactionType + String.valueOf(System.currentTimeMillis() - start)); } public Observable<String> obString() { return Observable.just("") .doOnNext(val -> { System.out.println("Thread " + Thread.currentThread() .getName()); }) .map(val -> "Hello"); } public Observable<String> obString1() { return Observable.just("") .doOnNext(val -> { System.out.println("Thread " + Thread.currentThread() .getName()); }) .map(val -> " World"); } public Observable<String> obString2() { return Observable.just("") .doOnNext(val -> { System.out.println("Thread " + Thread.currentThread() .getName()); }) .map(val -> "!"); } public Observable<String> obAsyncString() { return Observable.just("") .observeOn(scheduler) .doOnNext(val -> { System.out.println("Thread " + Thread.currentThread() .getName()); }) .map(val -> "Hello"); } public Observable<String> obAsyncString1() { return Observable.just("") .observeOn(scheduler1) .doOnNext(val -> { System.out.println("Thread " + Thread.currentThread() .getName()); }) .map(val -> " World"); } public Observable<String> obAsyncString2() { return Observable.just("") .observeOn(scheduler2) .doOnNext(val -> { System.out.println("Thread " + Thread.currentThread() .getName()); }) .map(val -> "!"); } (通用换行符)模式;你不应该再使用它了。而是使用U关键字参数。

The csv documentation声明它更喜欢该参数为newline=,因此''不会对换行符进行任何解释,并将其留给open模块。< / p>

由于您可能希望将CSV解码为 text (而非字节),因此在csv(二进制)模式下打开它们毫无意义。

底线:解析CSV文件的常用方法是:

b

这意味着您正在使用隐式with open('eggs.csv', newline='') as csvfile: spamreader = csv.reader(csvfile) for row in spamreader: ... 模式open,以便在文本模式下阅读。除非你有非常特殊的需求,否则这可能就是你想要的。以上示例代码直接来自文档。