3

I am importing csv file to django model with below commands:

if request.method == 'POST' and request.FILES['csv_file2']:
    myfile = request.FILES['csv_file2']
    fs = FileSystemStorage()
    filename = fs.save(myfile.name, myfile)
    data = csv.reader(fs.open(filename, mode='r'))

The problem that I am having that I can't see turkish characters. I searched at stackoverflow and I need to add utf-8 encoding while reading the csv file ,I tried several examples that I found but I couldn't make it work anywhere I tried to add encoding in my codes. Where should I add ?

  • I suppose you're using Python 2? If so, the `unicode_csv_reader` technique near the bottom of [this page](https://docs.python.org/2/library/csv.html) always helped me in those situations. – Paulo Almeida Apr 11 '18 at 17:58
  • I am using python 3 actually – İlkem Çetinkaya Apr 11 '18 at 18:00
  • the example at python 3 like as follows : import csv with open('some.csv', newline='', encoding='utf-8') as f: reader = csv.reader(f) for row in reader: print(row) While opening I need to add encoding='utf-8' but it's not working in my example (https://stackoverflow.com/questions/904041/reading-a-utf8-csv-file-with-python/904085#904085) – İlkem Çetinkaya Apr 11 '18 at 18:04
  • 1
    Yes, but that's with the built-in `open`, not `fs.open`, which apparently doesn't take an encoding parameter. You could try to get the path to the file and open it with built-in open, passing the encoding. I'm not used to working with `FileSystemStorage` directly, so I'm not sure about how it's usually done. – Paulo Almeida Apr 11 '18 at 18:08
  • 1
    Yes, you can get the path with [`fs.path`](https://docs.djangoproject.com/en/2.0/ref/files/storage/#django.core.files.storage.Storage.path). – Paulo Almeida Apr 11 '18 at 18:09
  • I got different problems when I used directly built-in open because of that I had to switch fs.open (The main problem was that built-in open couldn't reach the direct path of the file upload because it's dynamic but was working when it was static path.) – İlkem Çetinkaya Apr 11 '18 at 18:11
  • Hmm, Stack Overflow is suggesting to move this to chat, but I have to leave now. I don't know the specifics of why you need `fs.open`, but you could try calling `fs.path` immediately before the `open`, which presumably would get you the path that `fs.open` itself will use. Of course, it that path is not local it will fail, as the documentations warns. – Paulo Almeida Apr 11 '18 at 18:19
  • I saw your link seems useful,I will try that if I can't find any solution with fs.open.Thank you Paulo – İlkem Çetinkaya Apr 11 '18 at 18:26
  • 2
    @İlkemÇetinkaya use https://docs.python.org/2/library/io.html by setting the encoding in Python 2 if it's python 3, then that's the default. https://docs.python.org/3/tutorial/inputoutput.html – Saher Ahwal Apr 11 '18 at 18:26
  • why not use built in python `open` https://docs.python.org/3.6/library/functions.html#open takes encoding as parameter. – Saher Ahwal Apr 11 '18 at 18:27
  • Also are you sure your fs.save is saving with write encoding? you can simply use open to open for write. – Saher Ahwal Apr 11 '18 at 18:30
  • I am not using fs.save because I only read the file and write to django database. I am now checking the links that you sent – İlkem Çetinkaya Apr 11 '18 at 18:32
  • instead of data = csv.reader(fs.open(filename, mode='r')) I used fs.path = filename data = csv.reader(open(fs.path)) but I am not getting Exception Type: FileNotFoundError – İlkem Çetinkaya Apr 11 '18 at 19:49
  • I also modified as like as follows path= os.readlink(filename) with open(path) as f: data= f.read() still not able to read with built-in read method – İlkem Çetinkaya Apr 11 '18 at 20:02
  • 3
    @İlkemÇetinkaya The way to use `fs.path` would be: `path = fs.path(filename)` and then `csv.reader(open(path), encoding='utf8')`. – Paulo Almeida Apr 11 '18 at 21:41
  • this is not working too :( getting error at data = csv.reader(open(path), encoding='utf-8') : Exception Type: TypeError Exception Value: 'encoding' is an invalid keyword argument for this function – İlkem Çetinkaya Apr 12 '18 at 06:59
  • 1
    Sorry, that was a typo, the encoding is passed to `open` and not `csv.reader`. It's correct in ahmet's answer. – Paulo Almeida Apr 12 '18 at 08:53

1 Answers1

1

You need to modify as per below:

            path = fs.path(filename)
            with open(path, encoding='utf-8') as f:
                data = csv.reader(f)
                for row in data:
ahmet
  • 256
  • 2
  • 17