值不从 main 返回
Values not returning from main
我有以下代码,我希望将输入文件和输出文件引入到进一步的函数中,但是,这些代码似乎没有返回。仍然很新,如果这很简单,我们深表歉意。
# PYTHON 3.76 ONLY
# Version 0.0.1
import xml.etree.cElementTree as et
import pandas as pd
import sys, getopt
def main(argv):
inputfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print ('test.py -i <inputfile> -o <outputfile>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print ('test.py -i <inputfile> -o <outputfile>')
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
print ('Input file is "', inputfile)
print ('Output file is "', outputfile)
return inputfile, outputfile
if __name__ == "__main__":
main(sys.argv[1:])
# convert XML to dataframe
def xml2df(xml_data):
tree = et.parse(xml_data)
print (tree.getroot())
root = tree.getroot()
print ("tag=%s, attrib=%s" % (root.tag, root.attrib))
#iterate over each value for room and each user and add to rows
rows = []
for child in root.iter('rooms'):
roomId, roomTitle = 'id', 'ttl'
for it in child:
if it.tag == 'room':
roomId = it.findtext('roomID')
roomTitle = it.findtext('roomTitle')
roomStatus = it.findtext('status')
isAnonymous = it.findtext('isAnonymous')
elif it.tag == 'users':
rows.append([roomId, roomTitle, roomStatus, isAnonymous, it.findtext('uuid'), it.findtext('bbgEmail'),
it.findtext('corpEmail'), it.findtext('fullName'), it.findtext('firmName'), it.findtext('accountNumber'),
it.findtext('city'), it.findtext('accountName'), it.findtext('inviteDate'), it.findtext('removalDate')])
df = pd.DataFrame(rows, columns=['roomId', 'roomTitle', 'roomStatus', 'isAnonymous', 'uuid', 'bbgEmail',
'corpMail', 'fullName', 'firmName', 'accountNumber', 'city', 'accountName', 'inviteDate', 'removalDate'])
return df
df = xml2df(inputfile)
#output to csv
df.to_csv(ooutputfile+".csv", sep=',', index=False)
Error returned
Traceback (most recent call last): File "parse_pChatDump.py", line
63, in
df = xml2df(inputfile) NameError: name 'inputfile' is not defined
您在 main
方法中创建了 inputfile
和 outputfile
并返回了它,但是您没有将它们存储在脚本开始的变量中,因此您无法访问它们稍后,做:
def main(argv):
inputfile = ''
outputfile = ''
# ...
return inputfile, outputfile
def xml2df(xml_data):
# ...
return df
if __name__ == "__main__":
inputfile, outputfile = main(sys.argv[1:])
df = xml2df(inputfile)
df.to_csv(outputfile + ".csv", sep=',', index=False)
我有以下代码,我希望将输入文件和输出文件引入到进一步的函数中,但是,这些代码似乎没有返回。仍然很新,如果这很简单,我们深表歉意。
# PYTHON 3.76 ONLY
# Version 0.0.1
import xml.etree.cElementTree as et
import pandas as pd
import sys, getopt
def main(argv):
inputfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print ('test.py -i <inputfile> -o <outputfile>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print ('test.py -i <inputfile> -o <outputfile>')
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
print ('Input file is "', inputfile)
print ('Output file is "', outputfile)
return inputfile, outputfile
if __name__ == "__main__":
main(sys.argv[1:])
# convert XML to dataframe
def xml2df(xml_data):
tree = et.parse(xml_data)
print (tree.getroot())
root = tree.getroot()
print ("tag=%s, attrib=%s" % (root.tag, root.attrib))
#iterate over each value for room and each user and add to rows
rows = []
for child in root.iter('rooms'):
roomId, roomTitle = 'id', 'ttl'
for it in child:
if it.tag == 'room':
roomId = it.findtext('roomID')
roomTitle = it.findtext('roomTitle')
roomStatus = it.findtext('status')
isAnonymous = it.findtext('isAnonymous')
elif it.tag == 'users':
rows.append([roomId, roomTitle, roomStatus, isAnonymous, it.findtext('uuid'), it.findtext('bbgEmail'),
it.findtext('corpEmail'), it.findtext('fullName'), it.findtext('firmName'), it.findtext('accountNumber'),
it.findtext('city'), it.findtext('accountName'), it.findtext('inviteDate'), it.findtext('removalDate')])
df = pd.DataFrame(rows, columns=['roomId', 'roomTitle', 'roomStatus', 'isAnonymous', 'uuid', 'bbgEmail',
'corpMail', 'fullName', 'firmName', 'accountNumber', 'city', 'accountName', 'inviteDate', 'removalDate'])
return df
df = xml2df(inputfile)
#output to csv
df.to_csv(ooutputfile+".csv", sep=',', index=False)
Error returned
Traceback (most recent call last): File "parse_pChatDump.py", line 63, in df = xml2df(inputfile) NameError: name 'inputfile' is not defined
您在 main
方法中创建了 inputfile
和 outputfile
并返回了它,但是您没有将它们存储在脚本开始的变量中,因此您无法访问它们稍后,做:
def main(argv):
inputfile = ''
outputfile = ''
# ...
return inputfile, outputfile
def xml2df(xml_data):
# ...
return df
if __name__ == "__main__":
inputfile, outputfile = main(sys.argv[1:])
df = xml2df(inputfile)
df.to_csv(outputfile + ".csv", sep=',', index=False)