Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23218

is their any function that i can use to read my csv file without any codec error?

$
0
0

Is their any function that i can use to read my csv file without any codec error?

import refrom nltk.stem import WordNetLemmatizerimport contractionsimport csvbasicdict = dict()temp_of_colums = []def preprocess_data(test):    return testdef load_data():    sc_posrev = open("literally nothing\IMDBfileted.csv", mode="w", newline="")    prev = csv.DictWriter(sc_posrev, fieldnames=["review", "sentiment"])    cs_file = open("literally nothing\IMDB Dataset.csv",                   mode="r", encoding="cp1252")    csvfile = csv.DictReader(cs_file, delimiter=",")    line = 0    for row in csvfile:        # print(row)        kilo = preprocess_data(row["review"])        print(line)        if(line == 0):            prev.writeheader()        # print(row["review"])        line += 1        p = row["sentiment"].lower()        if(p == "positive"):            posrevl = []            prev.writerow({"review": kilo, "sentiment": 1})        elif(p == "negative"):            prev.writerow({"review": kilo, "sentiment": 0})        if(line == 4000):            break    cs_file.close()    sc_posrev.close()load_data()

Error:

    Traceback (most recent call last):  File "d:\nothing\literally nothing\stackov.py", line 100, in <module>    load_data()  File "d:\nothing\literally nothing\stackov.py", line 80, in load_data    for row in csvfile:  File "C:\Users\Varun\AppData\Local\Programs\Python\Python310\lib\csv.py", line 111, in __next__    row = next(self.reader)  File "C:\Users\Varun\AppData\Local\Programs\Python\Python310\lib\encodings\cp1252.py", line 23, in decode    return codecs.charmap_decode(input,self.errors,decoding_table)[0]UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 6237: character maps to <undefined>

line 211 refers to my function call and line 80 refers to iteratoriterating through the row in file it gives error and changing encodings gives me different types of errors


Viewing all articles
Browse latest Browse all 23218

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>