为啥函数没有返回值? [关闭]
Posted
技术标签:
【中文标题】为啥函数没有返回值? [关闭]【英文标题】:Why fuction's not returning value? [closed]为什么函数没有返回值? [关闭] 【发布时间】:2016-06-15 01:33:32 【问题描述】:这是我的完整代码。它提供了 rigt 输出,但它没有向函数 update2new 和 main 函数返回任何内容。在命令提示符下
嗨
从 [abc_20160301_(file1).csv] 获取数据.....
处理数据....
已删除 NIL..
成功执行[abc_20160301_(file1).csv]......
好的
但是成功执行的文件名应该像 "Successfully Executed[abc_20160301.csv]" 这是因为函数没有返回任何值。
import csv
import csv as csv
import os
import shutil
import xlrd
import re
from datetime import datetime, date, time, timedelta
import time
def update2new(src_dest):
print("Hi")
fileProcess(src_dest)
os.remove('outfile.csv')
os.remove('new.csv')
return src_dest
def fileProcess(src):
print('\nFetching Data From [ ] .....'.format(str(src)), end='\n')
#Removing first 6 rows
FIRST_ROW_NUM=1
ROWS_TO_DELETE = 1,2,3,4,5,6
with open(src, 'rt') as infile, open('outfile.csv', 'wt') as outfile:
outfile.writelines(row for row_num, row in enumerate(infile, FIRST_ROW_NUM)
if row_num not in ROWS_TO_DELETE)
print('\nProcessing Data....',end='\n')
#Removing NIL from the file
with open("outfile.csv") as f, open ('new.csv','wt') as out:
data = f.read()
new_data = re.sub("NIL"," ",data)
data = new_data
out.write(data)
print("Deleted NIL..")
customDate = (date.today()-timedelta(1)).strftime('%Y%m%d')
#Removing Total from last row
with open("new.csv") as f_origin, open ('D:\Pritam\PRS\abc_'+customDate+'.csv','wt') as dest:
for line in f_origin:
if not line.startswith('Total'):
dest.write(line)
return dest
def main():
print("<------Initializing Updating Process------>")
srcFile ='abc_20160301_(file1).csv'
update2new(srcFile)
print('\nSuccessfully Executed [ ]......'.format(str(srcFile)),end='\n')
print ("OK")
# ---------------------------------------------------------------------------------------------------
# This is THE standard boilerplate that calls THE main() function
if __name__ == '__main__':
main()
【问题讨论】:
update2new(srcFile)
在您的main
中。你似乎没有捕捉到return
...
【参考方案1】:
你的update2new
函数肯定会返回一些东西:
def update2new(src_dest):
print("Hi")
fileProcess(src_dest)
os.remove('outfile.csv')
os.remove('new.csv')
return src_dest
但是你没有在你的主目录中捕获它:
def main():
print("<------Initializing Updating Process------>")
srcFile ='abc_20160301_(file1).csv'
update2new(srcFile) #the return is NOT captured here
print('\nSuccessfully Executed [ ]......'.format(str(srcFile)),end='\n')
print ("OK")
改成:
def main():
print("<------Initializing Updating Process------>")
srcFile ='abc_20160301_(file1).csv'
srcFile = update2new(srcFile) #get the return here
print('\nSuccessfully Executed [ ]......'.format(str(srcFile)),end='\n') #then your printing should be OK
print ("OK")
【讨论】:
哦,谢谢哥们。在过去的 30 分钟里,那件愚蠢的事情让我很烦。 @PritamDas 这很长...@.@ 成功执行 [ <_io.textiowrapper name="D:\\Pritam\\abc_20160301.csv" mode="wt" encoding="cp1252"> ]...... 为什么显示输出中的模式和编码? @lan @PritamDas 您返回的是文件对象,而不是文件名。改为返回dest.name
。【参考方案2】:
如果要在调用函数时使用返回值,则需要将其影响到一个变量。示例:
def add_one(x):
return x+1
a = 1
b = add_one(a)
print(a, b) # outputs (1, 2)
【讨论】:
【参考方案3】:嗯,返回值必须存储在某个地方。 例如:
srcFile1=update2new(srcFile);
//returned value will be stored in srcFile
注意变化:
def main():
print("<------Initializing Updating Process------>")
srcFile ='abc_20160301_(file1).csv'
srcFile = update2new(srcFile)
print('\nSuccessfully Executed [ ]......'.format(str(srcFile1)),end='\n')
print ("OK")
或
def main():
print("<------Initializing Updating Process------>")
srcFile ='abc_20160301_(file1).csv'
print('\nSuccessfully Executed [ ]......'.format(str(update2new(srcFile))),end='\n')
print ("OK")
【讨论】:
以上是关于为啥函数没有返回值? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章