读取 Excel 个单元格并将内容复制到 txt 文件
Read Excel Cells and Copy content to txt file
我目前正在使用 RapidMiner,我正在尝试将 xlsx 文件中的 RapidMiner 结果复制到 txt 文件,以便使用 python 进行一些进一步处理。我在 A 列 (A1-A1500) 中有纯文本,在 C 列 (C1-C1500) 中有相应的文件名。
现在我的问题:
是否有可能(我正在考虑 xlrd 模块)读取 A 列中每个单元格的内容并将其打印到新创建的 txt 文件中,文件名在相应的 C 列中给出?
因为我从来没有使用过 xlrd 模块,所以我现在有点迷茫...
美好的一天!所以,我不确定我是否正确理解了您的问题,但是您是否尝试过将 Read Excel 运算符与 Loop Examples 运算符结合使用?然后,您的循环子流程可以使用 Write CSV 运算符或类似运算符。
关于 .xlsx 处理的每个任务,我都可以推荐 openpyxl。
对于您的要求:
from openpyxl import *
import os
p = 'path/to/the/folder/with/your/.xlsx'
files = [_ for _ in os.listdir(p) if _.endswith('.xlsx')]
for f in files:
wb = load_workbook(os.path.join(p, f))
ws = wb['name_of_sheet']
for row in ws.rows:
with open(row[2].value+'.txt', 'w') as outfile:
outfile.write(row[0].value)
感谢@corinna 最后的代码是:
from openpyxl import *
import os
p = r'F:\Results'
files = [_ for _ in os.listdir(p) if _ .endswith('.xlsx')]
os.chdir(r"F:\Results")
for f in files:
file_location = load_workbook(os.path.join(p, f))
sheet = file_location['Normal']
for row in sheet.rows:
with open(row[2].value + '.txt', "w") as outfile:
outfile.write(row[0].value)
我目前正在使用 RapidMiner,我正在尝试将 xlsx 文件中的 RapidMiner 结果复制到 txt 文件,以便使用 python 进行一些进一步处理。我在 A 列 (A1-A1500) 中有纯文本,在 C 列 (C1-C1500) 中有相应的文件名。 现在我的问题: 是否有可能(我正在考虑 xlrd 模块)读取 A 列中每个单元格的内容并将其打印到新创建的 txt 文件中,文件名在相应的 C 列中给出?
因为我从来没有使用过 xlrd 模块,所以我现在有点迷茫...
美好的一天!所以,我不确定我是否正确理解了您的问题,但是您是否尝试过将 Read Excel 运算符与 Loop Examples 运算符结合使用?然后,您的循环子流程可以使用 Write CSV 运算符或类似运算符。
关于 .xlsx 处理的每个任务,我都可以推荐 openpyxl。
对于您的要求:
from openpyxl import *
import os
p = 'path/to/the/folder/with/your/.xlsx'
files = [_ for _ in os.listdir(p) if _.endswith('.xlsx')]
for f in files:
wb = load_workbook(os.path.join(p, f))
ws = wb['name_of_sheet']
for row in ws.rows:
with open(row[2].value+'.txt', 'w') as outfile:
outfile.write(row[0].value)
感谢@corinna 最后的代码是:
from openpyxl import *
import os
p = r'F:\Results'
files = [_ for _ in os.listdir(p) if _ .endswith('.xlsx')]
os.chdir(r"F:\Results")
for f in files:
file_location = load_workbook(os.path.join(p, f))
sheet = file_location['Normal']
for row in sheet.rows:
with open(row[2].value + '.txt', "w") as outfile:
outfile.write(row[0].value)