2013-11-15 3 views
0

오전 모두 먼저이 질문과 유사한 게시물을 읽었지만 문제가 해결되지 않았습니다.파이썬 csv 파일 판독기가 전체 파일을 읽지 못함

3 개의 csv 파일 (중개인 4393 행, 건너 뛰기 27761 행, tippers 19118 행)이 있습니다. 각 CSV 파일과 같은 기능에 의해 판독된다 : 짧은

긴 층 (4393 행 포함)

브로커 CSV 파일 1,359 열의리스트를 생성한다. MISSING

skip csv 파일 (27761 개의 행 포함)은 27761 개의 행 목록을 생성합니다. FINE

tipper csv 파일 (19118 개의 행 포함)은 19118 개의 행 목록을 생성합니다. FINE

누구든지 수정 프로그램을 찾을 수 있습니까? 당신은 사방 전역을 많이 사용하는

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) 
+0

'fR.close()'줄은 불필요합니다. 왜냐하면'with' 문은 파일 열기와 닫기를 처리하기 때문입니다. – darthbith

답변

0

대답은 CSV 리더에 문제가 없다는 것이다. 차이는 위의 코드 라인에서 제외 되었기 때문에 목록 대신 4393의 1359 개 결과를 생산

if row[41] == '':   # exclude jobs that were cancelled. 

: 코드를보고 난 후에 나는 다음 줄을 보았다.

0

는 프로그램을 복잡하게, 그것을 넘어서 [아래 비용을 참조]. 함수는 필터링 된 목록을 만들고 반환 할 수 있으므로 다시 사용할 수 있으며 다른 매개 변수로 필터링하는 다른 함수에 입력으로 전달할 수 있습니다.

이 함수는 로컬 목록 expList를 만들고 아무 것도 반환하지 않습니다. CreateSkipList, CreateTipperList 함수에서도 마찬가지입니다. 목록

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]]) 

예는 적절한 방식으로 반환

def ReadEnquiry(file_to_read, rlist): 
    with open(file_to_read, "r") as fR: 
     r = csv.reader(fR) 
     for row in r: 
      rlist.append(row) 
    return rlist 

사용 예 :

rlist = [] 
read_list = ReadEnquiry(d + found[0], rlist) 
# pass read_list to other function as parameter 
brokerlist = [] 
CreateBrokerList(read_list, 48, 17, 74, brokerlist) 
관련 문제