Ronnie
Ronnie

Reputation: 3

Invalid characters seen while reading from a CSV file in Python 3.10

I am reading some URLs from a CSV file and just printing them in the console. When I print I see for some urls the '-' is being replaced by '–' in the console.

My Function to read csv is below :

def read_dict_csv():
    list_of_urls = []
    try:
        with open('csv/result.csv', 'r') as csv_file:
            csv_reader = csv.reader(csv_file)
            #next(csv_reader)
            for line in csv_reader:
                #print(line[0])
                list_of_urls.append(line[0])
    except FileNotFoundError as exp:
        print(exp.strerror, exp.filename)
    return list_of_urls

And my CSV file is as below:

https://cab.rbi.org.in/[enter image description here][1]docs/Training Cards/Eight Things About Agriculture Commodity Futures You May Want to Know.pdf
https://cab.rbi.org.in/docs/Training Cards/Exposure Norms for UCBs – Important RBI Guidelines.pdf

Upvotes: 0

Views: 413

Answers (1)

user18371825
user18371825

Reputation: 46

This is a issue with encoding. What worked for me is passing the encoding attribute on open so your code will be:

with open('csv/result.csv', 'r', encoding='utf8') as csv_file:

Upvotes: 2

Related Questions