我想用python制作一个程序,从csv文件生成所有可能的路径。我该怎么办?你知道吗
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
例如下图:
公司->;苹果->;iPhone->;iPhone4.0
公司->;三星->;平板电脑->;SamTab7.0
。。。你知道吗
公司->;索尼->;Xperia->;Xperi4K
。。。 链接到数据:https://docs.google.com/spreadsheets/d/11puxvQH6z6D3xiKybHEVbBpMp_Zv87LjlTd_ZIxYNw8/edit?usp=sharing
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
这似乎是一个家庭作业问题,但我发现它很有趣,所以我做了以下几点:
以及输出:
相关问题 更多 >
编程相关推荐