【发布时间】:2015-05-20 05:11:19
【问题描述】:
我有一个可以运行的应用程序。但是为了更好地理解函数和 python。我正在尝试将其拆分为各种功能。
我卡在 file_IO 函数上。我确定它不起作用的原因是因为应用程序的主要部分不理解 reader 或 writer。为了更好地解释。这是应用程序的完整副本。
我也对使用 csv.DictReader 和 csv.DictWriter 感到好奇。是否对当前代码提供任何优点/缺点?
我想另一种方法是通过类,老实说我也想知道如何做到这一点。
#!/usr/bin/python
""" Description This script will take a csv file and parse it looking for specific criteria.
A new file is then created based of the original file name containing only the desired parsed criteria.
"""
import csv
import re
import sys
searched = ['aircheck', 'linkrunner at', 'onetouch at']
def find_group(row):
"""Return the group index of a row
0 if the row contains searched[0]
1 if the row contains searched[1]
etc
-1 if not found
"""
for col in row:
col = col.lower()
for j, s in enumerate(searched):
if s in col:
return j
return -1
#Prompt for File Name
def file_IO():
print "Please Enter a File Name, (Without .csv extension): ",
base_Name = raw_input()
print "You entered: ",base_Name
in_Name = base_Name + ".csv"
out_Name = base_Name + ".parsed.csv"
print "Input File: ", in_Name
print "OutPut Files: ", out_Name
#Opens Input file for read and output file to write.
in_File = open(in_Name, "rU")
reader = csv.reader(in_File)
out_File = open(out_Name, "wb")
writer = csv.writer(out_File, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
return (reader, writer)
file_IO()
# Read header
header = reader.next()
stored = []
writer.writerow([header[0], header[3]])
for i, row in enumerate(reader):
g = find_group(row)
if g >= 0:
stored.append((g, i, row))
stored.sort()
for g, i, row in stored:
writer.writerow([row[0], row[3]])
# Closing Input and Output files.
in_File.close()
out_File.close()
【问题讨论】:
-
你有很多问题,其中一些对于 SO 来说是题外话(太宽泛)。请简化和澄清一个问题。
-
如果你可以让你的代码工作,那么关于清理它和改进样式的问题更适合 codereview 网站:codereview.stackexchange.com
-
好吧,那么现在的问题就是。使用当前代码,我收到以下错误消息 ./test.py Please Enter a File Name, (Without .csv extension): YouTubeVideoViewsDec2014 You enter: YouTubeVideoViewsDec2014 Input File: YouTubeVideoViewsDec2014.csv OutPut Files: YouTubeVideoViewsDec2014.parsed.csv Traceback (most最近通话最后):文件“./test.py”,第 56 行,在
header = reader.next() NameError: name 'reader' is not defined -
对于它的价值,我在这里写成函数的唯一代码是
find_group -
@KrisArmstrong 您的代码抛出特定错误的原因是,正如您在问题文本中提到的那样,您的主代码不知道
reader和writer是什么,因为它们是作用于代码块。你返回(reader, writer)这很好,但是当你调用函数时你必须分配它们,例如reader, writer = file_IO()。也就是说,以这种方式关闭这些文件处理程序将非常困难。我不会这样做。