2

I want to read data from a CSV file. The content of CVS file is

rekindle relight 
pesky annoying

Cell A1 contains rekindle
Cell B1 contains relight
Cell A2 contains pesky 
Cell B2 contains annoying

After reading the contain of the file I want to create a dictionary. The name of the dictionary is words_meanings. The name of the csv file is "words.csv"

import csv
file_name="words.csv"
words_meanings = {}
with open(file_name, 'r') as file:
    for line in file.readlines():
        key, value = line.split(",")
        words_meanings[key] = value.rstrip("\n")
print(words_meanings)

The output was : {'\ufeffrekindle': 'relight', 'pesky': 'annoying'} I want to removed \ufeff portion from the output. So I rewrite the code

import csv
file_name="words.csv"
words_meanings = {}
with open(file_name, newline='', encoding='utf-16-le') as file:

    for line in file.readlines():
        key, value = line.split(",")
        words_meanings[key] = value.rstrip("\n")
print(words_meanings)

Now I get 'utf-16-le' codec can't decode byte 0x0a in position 0: truncated data error.

How could I solve the problem?

Encipher
  • 1,370
  • 1
  • 14
  • 31

0 Answers0