Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

One string file Str.rc about French language is here:

ID_STR_BRIGHTNESS;,"Luminosité"
ID_STR_CHILE_EASTER_ISLAND;,"Île de Pâques"
ID_STR_CURRENT_CH;,"Saisie chaîne"
ID_STR_DETAILS;,"Détails"

......

Now I can export it to a Microsoft Str.xls like this:

cat ./Str.rc | sed 's/.*,//g' > ./Str.xls

But it will get,for example,'D茅tails' from 'Détails' in this way.

By the way,I try to get the file Str.rc encoding format via command: enca Str.rc, and it return as following:

enca: Cannot determine (or understand) your language preferences.
Please use `-L language', or `-L none' if your language is not supported
(only a few multibyte encodings can be recognized then).
Run `enca --list languages' to get a list of supported languages.

So,what can I do for this?

share|improve this question
1  
Use a real CSV parsing library like the ones available for Perl and the one in the Python standard library. They will take care of the quoted data (which might also contain newlines), special characters in strings and things like ';' as separator instead of ','. Only in extreme simplified cases can you use the Unix standard tools for handling CSV data. –  Anthon May 9 at 9:53
    
@ Anthon --I don't know all of about CSV and Python command at all,and it's impossable to do this in Unix? –  binghenzq May 9 at 10:16
    
You probably can but I found Unix to be ASCII oriented in many surprising places. It is just easier using the right tool for the job. –  Anthon May 9 at 10:36

1 Answer 1

up vote 1 down vote accepted

You can probably tweak your Unix tools to handle the encoding correctly. However if you just want to strip the data before the ',' using Python:

with open('Str.xls', 'w') as ofp:
   with open('Str.rc') as fp:
       for line in fp:
           ofp.write(line.split(',',1)[1])

If you want to run that from the commandline without saving it as a file first you can cut and paste:

python -c "with open('Str.xls', 'w') as ofp:
    with open('Str.rc') as fp:
       for line in fp:
           ofp.write(line.split(',',1)[1])"
share|improve this answer
    
@ Anthon -- I want to 'strip the data before the ',' using Python' which you offer,but get the return as follow: ID_STR_BRIGHTNESS;,"Luminosité" ID_STR_CHILE_EASTER_ISLAND;,"?le de Paques" ID_STR_CURRENT_CH;,"Saisie cha?ne" ID_STR_DETAILS;,"Détails" –  binghenzq May 9 at 10:59
    
Can cat the original file without problems? If not your terminal cannot deal with things. I updated the answer to have the output written directly. –  Anthon May 9 at 11:04
    
@ Anthon -- Of course,cat the original file has the same problems in terminal commandline 'cat Str.rc'. By the way,the original file encoding is UTF-8. –  binghenzq May 9 at 11:13
    
@ Anthon --Update the answer to output Str.xls,but the Str.xls have the same result,not the right strings. –  binghenzq May 9 at 11:22
    
@ Anthon --Thanks for your help anyway.I need to have dinner on this time,here is China. –  binghenzq May 9 at 11:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.