我有一个Python脚本,它读取.xls文件并使用循环来删除每行内部的所有不必要的返回。到目前为止,我的脚本可以遍历我指定的行并删除返回,但我希望它自动遍历每一行并删除所有不必要的返回。这是我的脚本 -
import xlrd
import xlwt
# function for removing returns in file
edits_returns = ''
def remove_returns1(row, column):
global edits_returns
cell_hold = sheet.cell(row, column).value
cell_hold_str = str(cell_hold)
if "\n" in cell_hold_str:
edits_returns = edits_returns + ('Return(s) replaced in (row %d : cell %d.)\n' % (row, column))
out_cell = cell_hold_str.replace('\n', '')
return out_cell
# obtaining filename
fname = raw_input('Input Filename > ')
# opening file
workbook = xlrd.open_workbook(fname)
sheet = workbook.sheet_by_index(0)
# informing user of # of rows and columns
print "\nNumber of rows: %d" % sheet.nrows
print "Number of Columns: %d\n" % sheet.ncols
# removing returns by row
column = 0
while column < sheet.ncols:
new_value = remove_returns1(34, column)
column += 1
print new_value,
# printing the edits
print "\n\n", edits_returns
我的问题
edit_results
中显示的修改结果? (我打算让这个脚本做的不仅仅是在将来删除退货)示例输入:
10/13/15 mcdonalds\n $20 0.01%
10/13/15 mcdonalds\n $20 0.01%
示例输出:
10/13/15 mcdonalds $20 0.01%
10/13/15 mcdonalds $20 0.01%
提供的答案之一的示例输出:
10/13/15 mcdonalds $20 0.01%10/13/15 mcdonalds $20 0.01%
这似乎很接近,但仍然不是我想要的。
提前致谢!我对所有建设性的批评持开放态度。
答案 0 :(得分:1)
替换
# removing returns by row
column = 0
while column < sheet.ncols:
new_value = remove_returns1(34, column)
column += 1
print new_value,
# printing the edits
print "\n\n", edits_returns
以下。您需要逐个遍历行,然后逐列。
# removing returns by row
row_idx =0
while row_idx < sheet.nrows:
col_idx = 0
while col_idx < sheet.ncols:
new_value = remove_returns1(row_idx, col_idx)
col_idx += 1
print new_value,
print
row_idx += 1
要将每一行存储到变量中,您需要先将该列附加到列表中,然后再将它们连接起来。
row_idx =0
while row_idx < sheet.nrows:
col_idx = 0
row_data =[]
while col_idx < sheet.ncols:
new_value = remove_returns1(row_idx, col_idx)
col_idx += 1
row_data.append(new_value)
a= ' '.join(row_data)
print a
row_idx += 1
如果您不想打印或立即使用它们,您也可以将'a'作为列表并将所有行附加到其中。