I am receiving the following error when I am reading a csv file in my Django project.
coercing to Unicode: need string or buffer, file found
I have searched the forum already, but could not find a solution to my problem. I also have no idea how to solve this. When I run this code locally I don't have the problem. Once I execute the script from my django views I get the error from above.
coercing to Unicode: need string or buffer, file found
import os
from django.conf import settings
path = os.path.join(settings.BASE_DIR, 'technicalAnalalysis/EUR_USD')
def index(request):
return render(request, 'technicalAnalysis/home.html')
class carouselData(APIView):
authentication_classes = []
permission_classes = []
def get(self, request, format=None):
import csv
EUR_USD_closeBid = []
with open(path) as csvfile:
readCSV = csv.reader(open(csvfile), delimiter=str(',').encode('utf-8'), )
for row in readCSV:
closeBid = row[0]
BOP = row[12]
EUR_USD_closeBid.append(closeBid)
data = {
"EUR_USD_closeBid":EUR_USD_closeBid,
}
return Response(data)
this is how the csv was created
master.to_csv(eachCurrency, encoding='utf-8', index=False)
and this how the csv data looks like
1.4551,2016-08-25T21:00:00.000000Z,1.46613370302,1.4588,1.45146629698,19.2623252118,16.0468541889,18.0294990319,52.3305843166,-0.000955558790503,0.00497888833433,0.00402332954383,-0.228110599078
1.45557,2016-08-28T21:00:00.000000Z,1.46022799941,1.45683,1.45343200059,18.3020335848,15.2573095743,17.1424034245,52.7163692506,-0.00105527020367,0.00471507078342,0.00365980057975,0.655172413793
1.45919,2016-08-29T21:00:00.000000Z,1.459965965,1.456766,1.453566035,17.7461955078,14.6233407849,18.0619294336,55.6907414516,-0.000874414835166,0.00449646707462,0.00362205223946,0.82183908046
Thanks a lot in advance and kind regards
You are opening the csv file twice. Remove the open()
from this line:
readCSV = csv.reader(open(csvfile), delimiter=str(',').encode('utf-8'), )
to
readCSV = csv.reader(csvfile, delimiter=str(',').encode('utf-8'))
You've already opened the file. You should read the file into memory then parse it with the csv library.
Otherwise, you can use csv.reader
instead, so you can skip opening the file and let the library handle it for you.
Perhaps csv.DictReader might also be helpful here. So for example:
with open(path) as f:
data = list(csv.DictReader(f))
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.