0

A former work colleague created a script a long time ago which among other things calls a Python file, this one retrieves information from two CSV files and merges them into one.

This script works great, no worries, and I needed to make some changes to it, just a couple of .replaces. The execution goes very well on my computer, but it does not work on the one for whom I made these modifications, there is an error message in the CMD and not being very good at Python I block, I of course tried research and several modifications, without success, each time the result is that the final file is found empty instead of containing all the data.

Suddenly it is the .replace which blocks, on my pc no worries, on his error and all words is not replaced.

My version of Python is 2.7.14 and his is 3.8.6, I guess the problem is there but I don't see what to change.

I tried this method without success: UnicodeEncodeError: 'charmap' codec can't encode characters

The Python file:

#coding:utf-8
import sys

file_name=sys.argv[1]
file=open(file_name,"r")

file = ''.join([i for i in file]) \
    .replace("clôturée", "Clôturée").replace(",",".").replace("Entitées", "").replace("Tous", "")

output=open(sys.argv[1],"w")
output.writelines(file)
output.close()

error in CMD: enter image description here

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 1
    open the program with 3.8 python set to default you will get a hint. – vuun0 Mar 11 '21 at 09:36
  • 2
    Please avoid posting images (or worse, links to images) of code or errors. Anything text-based (code and errors) should be posted as text directly in the question itself and formatted properly. You can get more [formatting help here](https://stackoverflow.com/help/formatting). You can also read about [why you shouldn't post images/links of code](//meta.stackoverflow.com/q/285551). – Tomerikoo Mar 11 '21 at 10:05

1 Answers1

0

The problem is most likely that you are sitting on different operating systems, and the open() function uses your local systems encoding as default. If you specify UTF-8 encoding when opening the file you would ensure that the text is encoded the same way no matter which OS the user of the script is sitting on.

Specify encoding like this:

file = open(file_name, "r", encoding="utf-8")
tbjorch
  • 1,544
  • 1
  • 8
  • 21