• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 319, comments - 22, trackbacks - 0, articles - 11
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            Parse .strings file with Python

            Posted on 2013-01-08 13:44 RTY 閱讀(593) 評論(0)  編輯 收藏 引用 所屬分類: Mac os 、轉(zhuǎn)載隨筆

            Parse .strings file with Python

            I'm trying to write a small Python script to parse the .strings file in my iPhone application project and determine which keys might not be in use. I'm, also doing some string matching to filter out some of the results. This is where my problems start :). If I try something like

            for file_line in strings_file: if 'search_keyword' in file_line: ...

            the search keyword will often not match, even though if I print every file line in the same for I seem to be reading the text correctly and my search keywords appear.

            The problem is these .strings files are in some binary format. Does anyone know of a proper way to parse these files?

            asked Jan 25 '10 at 12:49
            Mihai Damian
            2,51631736

            80% accept rate
            Can you post an example .strings file? – Tim Pietzcker Jan 25 '10 at 12:57
            They are pretty standard in format. I have lines like: "Keyword" = "Value"; or "Keyword" = 0.0; The files are correct as far as they're concerned. I use them from Objective-C and I have no problems there. It's just that there I use some framework provided class to open them which I probably can't use from Python – Mihai Damian Jan 25 '10 at 13:04
            So how do you open the file? What does printing the repr() of a line you think should match show you? – Thomas Wouters Jan 25 '10 at 13:05
            1 
            I open it with open(filename, 'r'). Also tried 'U'. 'b' is not supported on Mac OS – Mihai Damian Jan 25 '10 at 13:11
            It just occurred to me I might be able to use PyObjC to use the Objective-C code that reads these files. I'm still open to simpler solutions though – Mihai Damian Jan 25 '10 at 13:14
            show 1 more comment
            feedback

            Use correct encoding to open the .strings-file and in your source code. According to documentationthe encoding of your file could be utf-16.

            # -*- coding: utf-8 -*- import codecs  for line in codecs.open(u'your_file.strings', encoding='utf-16'): if u'keyword' in line: # process line
            answered Jan 25 '10 at 13:27
            J.F. Sebastian
            61.9k1096173
            Thanks, this works perfectly. Using your solution I realized some of my .strings were encoded utf-8 and most were utf-16. This probably explains why the matching worked only in some of the files. – Mihai Damian Jan 25 '10 at 13:55
            feedback

            No experience with those .strings files, but here is the reason why you don't find matches:

            strings_file.read()

            returns a string with the full content of the file. Iterating over a string iterates over single characters, i.e. in your for loop, file_line isn't a line, it's always just one single character (a string of length 1), which obviously can't contain a multi-character search word.

            answered Jan 25 '10 at 12:57
            balpha
            12.9k55177
            Indeed. Use for line in strings_file: instead. – Thomas Wouters Jan 25 '10 at 13:01
            Ah, you are right, I was using simply for file_line in strings_file:, without the read(); will edit in a moment – Mihai Damian Jan 25 '10 at 13:02
            @MihaiD: for line in file will only work (as expected) on plain text files, which (as you say) the.strings files are not. – balpha Jan 25 '10 at 13:05
            feedback

            It sounds like the stings file was saved as data. If python can't read it as is you can convert it to a plain text file in Objective-c.

            Just: (1) read the strings file into a file with the proper encoding. (2) Convert to dictionary (3) write dictionary to another file.

            So:

            NSString *strings=[NSString stringWithContentsOfFile:filePath encoding:NSUTF16StringEncoding error:&error]; NSDictionary *dict=[strings propertyList]; [dict writeToFile:anotherFilePath atomically:NO]; 
            answered Jan 25 '10 at 13:33
            TechZen
            42.3k74379
            Yes, that's one way to go. Will keep in mind if all else fails. – Mihai Damian Jan 25 '10 at 13:47
            feedback

            Your Answer

            亚洲va久久久噜噜噜久久| 99久久精品免费看国产| 久久人人爽人人爽人人AV东京热| 久久人做人爽一区二区三区| 亚洲欧美日韩中文久久 | 久久亚洲精品国产精品| 99精品国产在热久久无毒不卡| 国产精品伦理久久久久久| 国产精品久久久香蕉| 久久精品男人影院| 婷婷综合久久中文字幕蜜桃三电影| 国产成人综合久久久久久 | 欧美久久精品一级c片片| 久久久久久亚洲精品影院| 99久久精品国产一区二区三区| 亚洲AV无码1区2区久久 | 久久久久久亚洲Av无码精品专口 | 久久夜色精品国产欧美乱| 色偷偷88欧美精品久久久| 亚洲国产精品一区二区久久| 少妇人妻88久久中文字幕| 久久精品国产亚洲AV蜜臀色欲| 国内精品久久久久久久影视麻豆| 99久久国产综合精品麻豆| 日韩精品久久久久久免费| 国产亚洲精品久久久久秋霞| 久久人妻少妇嫩草AV蜜桃| 久久久精品日本一区二区三区| 国产A级毛片久久久精品毛片| 久久99精品国产麻豆宅宅| 久久精品国产免费| 国产99久久久国产精免费| 国产精品欧美亚洲韩国日本久久| 一本伊大人香蕉久久网手机| 久久99热狠狠色精品一区| 91麻精品国产91久久久久| 国产69精品久久久久99| 精品综合久久久久久88小说| 国产ww久久久久久久久久| 久久久久久综合一区中文字幕| 久久久久人妻精品一区|