(reprint) Game Planner Excel config table to turn into JSON file (i)

Source: Internet
Author: User

In the game client, it is generally impossible to read the planned Excel configuration table, which needs to be converted into a format that can be used, such as Json,xml format.

The tool I used is a Python script, the power of Python script is not mentioned here, a variety of cattle x mature Library ...

Execute the script as follows:

[CPP]View Plaincopy
  1. Import OS
  2. Import Sys
  3. Import Codecs
  4. Import JSON
  5. From XLRD import Open_workbook
  6. # "<type ' Unicode ' >"
  7. # "<type ' float ' >"
  8. def my_text_ (text):
  9. "" " Return the translated text from JSON text." ""  
  10. v = ("<type", "' >")
  11. if Text[:len (V[0])]! = V[0]: return text
  12. if Text[-len (V[1]):]! = v[1]: return text
  13. return Text[len (V[0]):-len (v[1])
  14. def sheet2json (sheet, jsonfile):
  15. Row = 0
  16. Attribute_row = []
  17. For col in Range (sheet.ncols):
  18. Attribute_row.append (Sheet.cell (Row, col). Value)
  19. attribute = {}
  20. Row=1
  21. For col in Range (sheet.ncols):
  22. Attribute[attribute_row[col]] = My_text_ (repr (Type (sheet.cell_value (row, col)))
  23. entities = []
  24. for row in range (2, sheet.nrows):
  25. entity = {}
  26. For col in Range (sheet.ncols):
  27. Entity[attribute_row[col]] = Sheet.cell (row, col). Value
  28. Row_dict = {}
  29. row_dict["Entity" = Entity
  30. Entities.append (row_dict)
  31. Sheet_dict = {}
  32. sheet_dict["entities"] = entities
  33. Filed = Codecs.open (".. /.. /jsonsfromexcel/"+jsonfile, " W ", " Utf-8 ") #输出目录
  34. Filed.write ("%s"% Json.dumps (sheet_dict, Ensure_ascii=false, indent=4))
  35. #print json.dumps (sheet_dict, Ensure_ascii=false, indent=4)
  36. Filed.close ()
  37. If Len (sys.argv)! = 2:
  38. Print "Usage:%s [input xls file]"% sys.argv[0]
  39. Print "e.g.:%s Myinput.xls"% sys.argv[0]
  40. Print "note:the input file should be MS Excel doc.\n"
  41. Sys.exit ()
  42. Xls_file = sys.argv[1]
  43. Workbook = Open_workbook (xls_file)
  44. For sheet in workbook.sheets ():
  45. Jsonfile = Sheet.name + ". JSON"
  46. #print "Converting%s '%s from EXCEL's sheet to JSON as%s"% (Xls_file, sheet.name, Jsonfile)
  47. Sheet2json (sheet, jsonfile)

You need to install the XLRD library in the system before using this command, a third-party library that parses Excel files

Original address: http://blog.csdn.net/zhuangyou123/article/details/10068729

(reprint) Game Planner Excel config table to turn into JSON file (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.