-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_text.py
More file actions
38 lines (30 loc) · 1.06 KB
/
extract_text.py
File metadata and controls
38 lines (30 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import pandas as pd
import json
import os
def extract_text_field():
# 读取CSV文件
input_file = r'E:\git_pull\阿拉伯\处理完成最后.csv'
output_file = r'E:\git_pull\阿拉伯\处理完成结果\提取text结果余下所有.csv'
# 确保输出目录存在
os.makedirs(os.path.dirname(output_file), exist_ok=True)
# 读取CSV文件
df = pd.read_csv(input_file)
# 提取第二列中的text字段
def extract_text(json_str):
try:
# 解析JSON字符串
data = json.loads(json_str)
# 返回text字段的值
return data.get('text', '')
except:
return ''
# 应用提取函数到第二列
df['生成结果'] = df['生成结果'].apply(extract_text)
# 保存到新的CSV文件
df.to_csv(output_file, index=False, encoding='utf-8')
# 打印统计信息
print(f"处理完成!")
print(f"原始文件行数: {len(df)}")
print(f"结果已保存到: {output_file}")
if __name__ == "__main__":
extract_text_field()