xlin 0.1.1__tar.gz → 0.1.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: xlin
3
- Version: 0.1.1
3
+ Version: 0.1.2
4
4
  Summary: toolbox for LinXueyuan
5
5
  License: MIT
6
6
  Author: XiChen
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "xlin"
3
- version = "0.1.1"
3
+ version = "0.1.2"
4
4
  description = "toolbox for LinXueyuan"
5
5
  authors = ["XiChen <23211526+LinXueyuanStdio@users.noreply.github.com>"]
6
6
  license = "MIT"
@@ -47,6 +47,28 @@ def jsonlist_to_dataframe(json_list: List[Dict[str, str]]):
47
47
  return pd.DataFrame(json_list)
48
48
 
49
49
 
50
+ def is_jsonl(filepath: str):
51
+ with open(filepath) as f:
52
+ try:
53
+ l = next(f) # 读取一行,用来判断文件是json还是jsonl格式
54
+ f.seek(0)
55
+ except:
56
+ return False
57
+
58
+ try:
59
+ _ = json.loads(l)
60
+ except ValueError:
61
+ return False # 第一行不是json,所以是json格式
62
+ else:
63
+ return True # 第一行是json,所以是jsonl格式
64
+
65
+
66
+ def load_json_or_jsonl(filepath: str):
67
+ if is_jsonl(filepath):
68
+ return load_json_list(filepath)
69
+ return load_json(filepath)
70
+
71
+
50
72
  def load_json(filename: str):
51
73
  with open(filename, "r", encoding="utf-8") as f:
52
74
  return json.load(f)
@@ -66,6 +88,8 @@ def load_json_list(filename: str):
66
88
  try:
67
89
  obj = json.loads(i.strip())
68
90
  except:
91
+ print("格式损坏数据,无法加载")
92
+ print(i)
69
93
  continue
70
94
  json_list.append(obj)
71
95
  return json_list
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes