【发布时间】:2013-11-15 12:15:02
【问题描述】:
大家早上好, 首先,我已经阅读了与此问题类似的帖子,但它并没有解决我的问题。
我有 3 个 csv 文件(broker-4393 行;skips-27761 行;tippers-19118 行)。 每个 csv 文件都由同一个函数读取:
长话短说:
broker csv 文件(包含 4393 行)生成一个包含 1359 行的列表。缺少
跳过 csv 文件(包含 27761 行)生成 27761 行的列表。很好
tipper csv 文件(包含 19118 行)生成一个包含 19118 行的列表。很好
有人设法找到解决办法吗?
[见下面的费用]
import os, re, csv
# -------------------------------------- General Functions -------------------------------------- #
# function: find journey summary file
def FileFinder(fl, begin):
regex = begin
pattern = re.compile(regex)
for f in fl:
if re.findall(pattern, f): #empty seq = False
global found;
found = re.findall(pattern, f)
# function: read from 'Enquiry-...'
def ReadEnquiry():
with open(d + found[0], "r") as fR:
r = csv.reader(fR)
# capture data from csv file into 'clist'
for row in r:
global rlist;
rlist.append(row)
fR.close()
# ----------------------------------------------------------------------------------------------- #
# --------------------------------------- Broker Functions -------------------------------------- #
# function: Find and Read from BrokerExport.
def BrokerExp():
FileFinder(filelist, 'BrokerExport.*')
ReadEnquiry()
CreateBrokerList(rlist, 48, 17, 74, brokerlist)
# function: create a list of broker data. Format: Account Number,Date,Price(ex-VAT),Profit
def CreateBrokerList(rlist, col1, col2, col3, expList):
for row in rlist:
if row[41] == '': # exclude jobs that were cancelled.
expList.append([row[col1], row[col2], row[col3]])
# ----------------------------------------------------------------------------------------------- #
# ---------------------------------------- Skip Functions --------------------------------------- #
# function: Find and Read from SkipsExport.
def SkipExp():
FileFinder(filelist, 'SkipsExport.*')
ReadEnquiry()
CreateSkipList(rlist, 2, 42, 46, skiplist)
# function: create a list of skip data. Format: Account Number,Date,Price(ex-VAT),Profit
def CreateSkipList(rlist, col1, col2, col3, expList):
for row in rlist:
expList.append([row[col1], row[col2], row[col3]])
# ----------------------------------------------------------------------------------------------- #
# ---------------------------------------- Skip Functions --------------------------------------- #
# function: Find and Read from TipperExport.
def TipperExp():
FileFinder(filelist,'TipperExport.*')
ReadEnquiry()
CreateSkipList(rlist,3,4,34,tipperlist)
# function: create a list of tipper data. Format: Account Number,Date,Price(ex-VAT),Profit
def CreateTipperList(rlist, col1, col2, col3, expList):
for row in rlist:
expList.append([row[col1], row[col2], row[col3]])
# ----------------------------------------------------------------------------------------------- #
# --- General Variables --- #
rlist = []; # 'rlist' list read from csv.
found = '' # string to hold filename found through 'FileFinder()'
d = 'U:/rmarshall/To Do/' # directory to use
headings = ['Company Name', 'Rep', \
'Month 1 Calls', 'Month 1 Inv Tots', 'Month 1 No. of Invs', \
'Month 2 Calls', 'Month 2 Inv Tots', 'Month 2 No. of Invs', \
'Month 3 Calls', 'Month 3 Inv Tots', 'Month 3 No. of Invs', \
'Month 4 Calls', 'Month 4 Inv Tots', 'Month 4 No. of Invs', \
'Month 5 Calls', 'Month 5 Inv Tots', 'Month 5 No. of Invs', \
'Month 6 Calls', 'Month 6 Inv Tots', 'Month 6 No. of Invs', \
'Month 7 Calls', 'Month 7 Inv Tots', 'Month 7 No. of Invs', \
'Month 8 Calls', 'Month 8 Inv Tots', 'Month 8 No. of Invs', \
'Month 9 Calls', 'Month 9 Inv Tots', 'Month 9 No. of Invs', \
'Month 10 Calls', 'Month 10 Inv Tots', 'Month 10 No. of Invs', \
'Month 11 Calls', 'Month 11 Inv Tots', 'Month 11 No. of Invs', \
'Month 12 Calls', 'Month 12 Inv Tots', 'Month 12 No. of Invs']
cp=[headings]; da=[headings]; mb=[headings]; apd=[headings]; bobs=[headings] # separate Rep lists
filelist=os.listdir(d) # place directory filenames into a list
dlist=[]; brokerlist=[]; skiplist=[]; tipperlist=[]; book1=[] # lists used throughout code
brklist=[]; skplist=[]; tprlist=[] # a list of names
# ------------------------- #
# --- main --- #
Enquiry_Main() # call 'Enquiry_Main()' to run all work to create 'cp,da,mb,apd,bob' list data.
rlist=[]; dlist=[] # reset lists
print('1')
BrokerExp() # call 'BrokerExp()' to run all work to create 'brokerlist' data.
rlist=[] # reset list
print('2')
SkipExp() # call 'SkipExp()' to run all work to create 'skiprlist' data.
rlist=[] # reset list
print('3')
TipperExp() # call 'TipperExp()' to run all work to create 'tipperlist' data.
rlist=[] # reset list
a=0
for row in brokerlist:a+=1
print(a)
a=0
for row in skiplist:a+=1
print(a)
a=0
for row in tipperlist:a+=1
print(a)
【问题讨论】:
-
一件小事,
fR.close()行是不必要的,因为with语句处理打开和关闭文件。
标签: python csv python-3.x