Created
May 26, 2014 14:53
-
-
Save cnsoft/1bc4d74df57d18f93839 to your computer and use it in GitHub Desktop.
export some item from one json file to new file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import codecs | |
#open the source file | |
json_file = open('map.txt','r') | |
data = json_file.read() | |
datas = json.loads(data.decode("utf-8" ) ) | |
#data1 = json.loads(json_file.read())#.decode('utf-8') ) | |
# data.decode("utf-8-sig")) | |
#print data | |
print type(datas) | |
map_units = datas["units_inst"] | |
print type(map_units) | |
targetmap = "map_3" | |
out_units = [] | |
for unit in map_units: | |
if( unit["map_name"] == targetmap): | |
out_units.append(unit) | |
print len(out_units) | |
#out put wanted units to template data.. | |
out_dicts = {"units_inst": out_units} | |
output_file = codecs.open("output_file.json", "w", encoding="utf-8",buffering=0) | |
json.dump(out_dicts, output_file, indent=2, sort_keys=True, ensure_ascii=False) | |
print "done" | |
#confirm file ok. | |
json_file2 = open("output_file.json",'r') | |
data = json_file2.read() | |
datas = json.loads(data.decode("utf-8")) | |
print len(datas["units_inst"]) | |
Call this to remove bom header of utf-8 file
import codecs
import shutil
import sys
s = sys.stdin.read(3)
if s != codecs.BOM_UTF8:
sys.stdout.write(s)
shutil.copyfileobj(sys.stdin, sys.stdout)
named it as remove_bom.py
how this code is work? $ remove_bom.py < input.txt > output.txt
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is my implementation to convert any kind of encoding to UTF-8 without BOM and replacing windows enlines by universal format:
def utf8_converter(file_path, universal_endline=True):
'''
Convert any type of file to UTF-8 without BOM
and using universal endline by default.