Files
F6--/其它系统脚本/单元格多行内容拆分.ipynb
T
2026-01-30 11:28:35 +08:00

66 lines
1.9 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"# #读取Excel表格\n",
"df = pd.read_excel(r\"C:\\Users\\admin\\Desktop\\会员资料自动备份_20230329.zip(1)\\会员资料自动备份_20230329.zip(1)\\套餐卡全量.xlsx\")\n",
"df_ = pd.DataFrame()\n",
"#将要拆分的列的编号\n",
"col_index = 5\n",
"col_index2 = 6\n",
"#遍历每个单元格\n",
"new_rows = []\n",
"for i, row in df.iterrows():\n",
" #读取单元格的文本\n",
" text = str(row[col_index])\n",
" text2 = str(row[col_index2])\n",
" #检查是否有需要拆分的文本\n",
" if '\\n' in text:\n",
" #将文本拆分成多行\n",
" lines = text.split('\\n')\n",
" lines2 = text2.split('\\n')\n",
" #将多行文本插入到新的行中\n",
" for j in range(len(lines)):\n",
" new_row = row.copy()\n",
" new_row[col_index] = lines[j]\n",
" new_row[col_index2] = lines2[j]\n",
" new_rows.append(new_row)\n",
" else:\n",
" new_row = row.copy()\n",
" new_rows.append(new_row)\n",
" print(i)\n",
"#将处理后的数据保存到Excel表格中\n",
"df_ = df_.append(new_rows, ignore_index=True)\n",
"df_.to_excel('输出文件.xlsx', index=False)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "F6processing",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.4"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}