简体   繁体   中英

Python - Read remote CSV file with utf-8-sig encoding

I need to read a remote CSV file that I've uploaded on my server. I tried the following way, the file is read as text but it's not reading all the rows, it's reading the data in a strange way, like splitting the strings. The file has semi colon delimiters and is encoded with utf-8-sig .

import csv
import pprint
import urllib


url = "http://myfakesite.com/mycsv.csv"
r = urllib.request.urlopen(url)
csv_file = r.read().decode(encoding="utf-8-sig")
reader = csv.DictReader(csv_file, delimiter=";")
for row in reader:
    pprint(row["Choice"])

Is there any other way to read the file without having problems?

What about using pandas?

import pandas
csv = pandas.read_csv('http://myfakesite.com/mycsv.csv', encoding='utf-8-sig')
print(csv['Choice'].tolist())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM