2017-07-13 1 views
0

Pythonの新機能といくつかのガイダンスを探しています。私は (各店舗のために1つの)フォルダ内の何百ものテキストファイルをループし、ストアID(テキスト文書のタイトルで与えられたxxx2902ncjc)を持つCSVファイルを生成しようとしています。 (すなわち、maxPeople = 31、またはspace_temp = 78など)。各テキストファイルには場所によって異なるパラメータがある可能性があるので、私は以下のforループの一意の変数をすべて取得しました。 2番目のfor-loopにあるすべてのストアIDを取得しました。それは私が今までに得たすべてです。テキストファイルでいっぱいのループスルーフォルダー、変数の値を取得、CSVとstoreIDと変数名を一致させて埋め込みます。

1)このすべてをExcelにインポートする方法を理解する、2)正しいパラメータでIDを格納することを各ファイル名のスライスに保存する方法3)ストアIDと変数へのパラメータを一致させる優れた方法です。

私は次に何をすべきか分かりません。私はsuuuper初心者ですので、すべての助けは非常に高く評価されるでしょう。乾杯。

import os, sys, glob 
path = r"C:\Users\XXXXX" #insert folder for data here 
dirs=os.listdir(path) 
fullfilenames=[] 
variablelist=[] 
allvariables=[] 
variables=[] 
for file in os.listdir(path): 
    if ".prop" in file: 
     fullfilenames.append(path+'\\'+file) 

for name in fullfilenames: #create list of StoreIDs 
    index_of_c = name.index('qPA') 
    file_name= name[index_of_c:] #cuts off path 
    file_name=file_name.rsplit(".",1)[0] #removes extension 
    SiteID= file_name[4:] #splits filename into Site ID 
    print (SiteID) #prints SiteID 

for file in fullfilenames: 
    f = open(file,'r') #opens the file and enters reading mode 
    content=f.readlines() #reads each line of file and seperates based on whitespace 
    for line in content: 
     variables.append(line.split('=')[0]) #splits up each line of each file, specified before the "=" 
     for variable in variables: 
      if variable not in allvariables: #checks if variable is included in the variable list 
       allvariables.append(variable) #if variabe isn't include in the list, it adds it to list 


def createkeys(): 

print(allvariables) 
print(type(allvariables)) 
print(len(allvariables)) 
+0

[編集]あなたの質問と '.prop'ファイルとあなたの予想出力からのデータの少なくとも3行を示しています。 – stovfl

答えて

0
import os, sys, glob, re 
path = r"C:\Users\mcantwell\Desktop\Projects\kohls_prop" #insert folder for data here 
outfile = r"C:\Users\mcantwell\Desktop\Projects\kohls_prop.csv" 
dirs=os.listdir(path) 
fullfilenames=[] 
variablelist=[] 
allvariables=set() 
variables=[] 
for file in os.listdir(path): 
    if ".prop" in file: 
     fullfilenames.append(path+'\\'+file) 

for file in fullfilenames: 
    f = open(file,'r') #opens the file and enters reading mode 
    content=f.readlines() #reads each line of file and seperates based on whitespace 
    for line in content: 
     line_split = line.split('=') #splits up each line of each file, specified before the "=" 
     if len(line_split) == 2: 
      variable = line_split[0] 
      allvariables.add(variable) 

out = open(outfile, 'w') 

def writerow(row): 
    out.write(', '.join(row)) 
    out.write('\n') 

writerow(['SiteID'] + list(allvariables)) 

for file in fullfilenames: 
    m = re.search('qPAC(\d+)', file) 
    SiteID = m.group(1) 
    f = open(file,'r') #opens the file and enters reading mode 
    content=f.readlines() #reads each line of file and seperates based on whitespace 
    data={} 
    for line in content: 
     line_split = line.strip().split('=') #splits up each line of each file, specified before the "=" 
     if len(line_split) == 2: 
      variable = line_split[0] 
      value = line_split[1] 
      data[variable] = value 
    values = [SiteID] + [data.get(variable, '') for variable in allvariables] 
    writerow(values) 

print(allvariables) 
print(type(allvariables)) 
print(len(allvariables)) 
関連する問題