2017-07-17 16 views
1

私はcsvファイルからすべての可能なパスを生成するpythonを使ってプログラムを作成します。私はこれについてどうやって行くのですか? - >アップル - > iPhone - > iPhone4.0CSVファイルからPythonを使ってすべてのパスを生成する

会社 - >サムスン - > Gtablet - > SamTab7.0

会社:下の画像では例えば

import pandas as pd 
import csv 

def dataInCol(fieldName): 
    df = pd.read_csv('..\\data.csv',usecols=[fieldName]) 
    qr = df.values.tolist() 
    flattened = [val for sublist in qr for val in sublist] 
    return flattened 


def makingPaths(dataCol, path = "Companies: "): 
    with open('..\\data.csv', "rb") as f: 
     reader = csv.DictReader(f) 
     headers = reader.fieldnames 
    for eachValue in dataCol: 
     path = path + str(eachValue) 
     if str(eachValue) in headers: 
      newCol = dataInCol(str(eachValue)) 
      makingPaths(newCol) 
     break 
    return path 

...

会社 - >ソニー - >のXperia - > Xperia4K

...データへリンク:https://docs.google.com/spreadsheets/d/11puxvQH6z6D3xiKybHEVbBpMp_Zv87LjlTd_ZIxYNw8/edit?usp=sharing

CSV Data Image

Companies Apple Samsung Sony iPhone Mac iPad Galaxy Notebook Gtablet Xperia Xtablet 
Apple iPhone Galaxy Xperia iPhone4.0 Macbook iPadMini GalaxyS3 NSeries5 SamTab7.0 Xperia4K XTab6.0 
Samsung Macbook Notebook Xtablet iPhone4.7 MacPro iPadReg GalaxyS4 NSeries7 SamTab9.0 XperiaUltra XTab8.0 
Sony iPad Gtablet  iPhone5.5 MacBookPro iPadPro GalaxyS8 NSeries9  XperiaPrem XTab10.0 
+1

csvをテキストとして投稿すると、改善されます... –

+0

データをテキストとして追加し、詳細を説明し、これまでのコードを追加してください。 –

+0

「パス」はどういう意味ですか? 'str'オブジェクトか何か? –

答えて

0

これは宿題の質問のように思えるが、私はそれはので、ここで私がやったことだ興味深いが見つかりました:

csvfile = open('data.csv', 'r') #open the csv file 
lines = csvfile.readlines() # read the file into a list, one line per element 
headings = [cell.strip() for cell in lines[0].split(",")] #keep a clean list of headings 
splitlines = [] 
for line in lines[1:]: # split each row into cells 
    splitlines.append(line.split(",")) 
columns = zip(*splitlines); # convert rows to columns 

def recursePrint(colval, text): # a recursive function to traverse the columns 
     colval = colval.strip() 
     if (colval in headings): 
      for c in columns[headings.index(colval)]: 
       newtext = text + colval + "->" 
       recursePrint(c, newtext) 
     else: 
      print (text + colval) 


for x in columns[0]: 
    recursePrint(x, "") # run the recursion 

そして出力:

Apple->iPhone->iPhone4.0 
Apple->iPhone->iPhone4.7 
Apple->iPhone->iPhone5.5 
Apple->Macbook 
Apple->iPad->iPadMini 
Apple->iPad->iPadReg 
Apple->iPad->iPadPro 
Samsung->Galaxy->GalaxyS3 
Samsung->Galaxy->GalaxyS4 
Samsung->Galaxy->GalaxyS8 
Samsung->Notebook->NSeries5 
Samsung->Notebook->NSeries7 
Samsung->Notebook->NSeries9 
Samsung->Gtablet->SamTab7.0 
Samsung->Gtablet->SamTab9.0 
Samsung->Gtablet-> 
Sony->Xperia->Xperia4K 
Sony->Xperia->XperiaUltra 
Sony->Xperia->XperiaPrem 
Sony->Xtablet->XTab6.0 
Sony->Xtablet->XTab8.0 
Sony->Xtablet->XTab10.0 
Sony-> 
関連する問題