Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Convert csv file from us-ascii to UTF-8

I'm just trying to convert a file over I run this through terminal


StudioA:~ StudioA$ file -I /Users/StudioA/Desktop/Mikey_WK37.csv

and the result is

/Users/StudioA/Desktop/Mikey_WK37.csv: text/plain; charset=us-ascii

So I run this

iconv -f US-ASCII -t UTF-8 /Users/StudioA/Desktop/Mikey_WK37.csv > /Users/StudioA/Desktop/new_file.csv

then when I check the new file it is still in us-ascii.

Where am I going wrong?

Mac Pro, OS X El Capitan (10.11.1)

Posted on May 5, 2016 6:29 AM

Reply
18 replies

Jun 2, 2016 7:53 AM in response to Hiroto



-- APPLESCRIPT set infile to (choose file)'s POSIX path set outfile to (choose file name default name "out.txt")'s POSIX path --set infile to "/path/to/in.txt" --set outfile to "/path/to/out.txt" do shell script "/bin/bash -s <<'EOF' - " & infile's quoted form & space & outfile's quoted form & " # effectively prepend UTF-8 BOM signature to $1 and output to $2 iconv -t UTF-16 \"$1\" | iconv -f UTF-16BE -t UTF-8 > \"$2\"" -- END OF APPLESCRIPT


I'm using this, how can I write it back to the same file or create a file of the same name. I don't need to keep the original.


Matt

Jun 2, 2016 11:47 PM in response to MattJayC

Hello


You may try something like this.



set f to (choose file)'s POSIX path --set f to "/path/to/a.txt" add_utf8_bom(f) on add_utf8_bom(f) (* string f : POSIX path of text file in UTF-8 *) do shell script "file=" & f's quoted form & " [[ $(head -c3 \"$file\") == $'\\xef\\xbb\\xbf' ]] && exit # UTF-8 BOM already exists temp=$(mktemp /tmp/iconv.XXXXXX) || exit iconv -f UTF-8 -t UTF-16 \"$file\" > \"$temp\" && iconv -f UTF-16BE -t UTF-8 \"$temp\" > \"$file\" err=$? rm \"$temp\" exit $err" end add_utf8_bom




Briefly tested under OS X 10.6.8.


Regards,

H

Convert csv file from us-ascii to UTF-8

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.