使用Python从CSV文件生成所有路径
我想用python制作一个程序,从csv文件生成所有可能的路径。我该怎么办使用Python从CSV文件生成所有路径,python,csv,path,Python,Csv,Path,我想用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
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
公司->三星->Gtablet->SamTab7.0
公司->索尼->Xperia->Xperia4K
。。。
链接到数据:
这似乎是一个家庭作业问题,但我发现它很有趣,所以我做了以下几点:
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->
这似乎是一个家庭作业问题,但我发现它很有趣,所以我做了以下几点:
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->
将csv发布为文本将是一项改进…请将您的数据添加为文本,详细说明您正在尝试执行的操作,并添加到目前为止的代码。您所说的“路径”是什么意思?一个
str
对象还是什么?@AzatIbrakov是一个strobject@Jean-Françoisfare I发布了一个数据链接,将csv发布为文本将是一个改进…请将您的数据添加为文本,详细解释您正在尝试做什么,并添加到目前为止的代码。您所说的“路径”是什么意思?一个str
对象还是什么?@AzatIbrakov是一个strobject@Jean-弗朗索瓦·法布雷一世发布了数据链接