0

I have a CSV file which has two columns 'title' and 'description' . The Description columns has HTML elements . I am trying to replace 'InterviewNotification' with InterviewAlert .

screenshot here of csv file

This is the code i wrote :

text = open("data.csv", "r")
text = ''.join([i for i in text]).replace("InterviewNotification", "InterviewAlert")
x = open("output.csv","w")
x.writelines(text)
x.close()

But, Im getting this Error :

      File "C:\Users\Zed\AppData\Local\Programs\Python\Python39\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 0x9d in position 5786: character maps to <undefined>

Also used pandas , here is the code :

dataframe = pd.read_csv("data.csv")  
# using the replace() method 
dataframe.replace(to_replace ="InterviewNotification", value = "InterviewAlert",  inplace = True) 

still no Luck. Help pls

2
  • as the raised exception says, you have a UnicodeError. that means your original data is "malformed", i.e. there are char bytes which cannot be decoded with the encoding you're using, which is UTF by default. your data is probably not using it, so you should check the original document encoding, then read the file with open(path, 'rb') and decode the resulting bytes string with the correct format Commented Dec 9, 2020 at 18:28
  • Please do not mark your own question text as citations with the vertical bar in front created by > when editing. Commented Dec 9, 2020 at 18:47

2 Answers 2

0

Have you tried specifying the encoding as "utf-8" in your first line? For example:

text = open("data.csv", encoding="utf8")

It seems that your issue may be related to this question

Sign up to request clarification or add additional context in comments.

1 Comment

i did encoding='utf-8-sig' and it worked.
0

You open the file but you do not read it. To get the text itself do this:

textFile = open("data.csv", "r")
text = textFile.read()
textFile.close()

Or, to improve the code, use a context manager:

with open("data.csv", "r") as textFile:
    text = textFile.read()

This ensures that the file is properly closed even if the intermediate code raises an exception.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.