如何解决二进制模式不带编码参数

Nou*_*ros 5 python nltk

码:

import nltk
eng_lish= open("C:/Users/Nouros/Desktop/Thesis/english.csv","rb", encoding='utf8').read()
bang_lish= open("C:/Users/Nouros/Desktop/Thesis/banglish.csv","rb", encoding='utf8').read()
Run Code Online (Sandbox Code Playgroud)

问题:

import nltk
eng_lish= open("C:/Users/Nouros/Desktop/Thesis/english.csv","rb", encoding='utf8').read()
bang_lish= open("C:/Users/Nouros/Desktop/Thesis/banglish.csv","rb", encoding='utf8').read()
Run Code Online (Sandbox Code Playgroud)

Jea*_*bre 5

您正在读取csv文件,即文本文件。因此,您需要编码,但不需要二进制模式。

因此,您不应使用rb它们来打开它们(建议csv在Python 2中使用模块时这样做,但在其他情况下无关紧要)。

只需使用纯文本模式:

open("C:/Users/Nouros/Desktop/Thesis/english.csv","r", encoding='utf8').read()
Run Code Online (Sandbox Code Playgroud)

我我更喜欢使用csv模块,以避免手动拆分行和列:

import csv
with open(r"C:\Users\Nouros\Desktop\Thesis\english.csv","r", encoding='utf8') as f:
     cr = csv.reader(f,delimiter=",") # , is default
     rows = list(cr)  # create a list of rows for instance
Run Code Online (Sandbox Code Playgroud)

(请注意,csv模块建议在使用newline=""Python 3打开文件进行读取时使用,但实际上是在写入文件时出现的问题)