【发布时间】:2017-04-04 23:49:15
【问题描述】:
我有一个已导出为 CSV 的 Excel 文档。它看起来像这样:
"First Name","Last Name","First Name","Last Name","Address","City","State"
"Bob","Robertson","Roberta","Robertson","123 South Street","Salt Lake City","UT"
"Leo","Smart","Carter","Smart","827 Cherry Street","Macon","GA"
"Mats","Lindgren","Lucas","Lindgren","237 strawberry xing","houston","tx"
我有一个名为“Category”的类,它有一个名称变量。我的代码为每个第一行字符串创建了一个类别,但现在我需要将每个项目添加到它应该进入的列中。
import xlutils
from difflib import SequenceMatcher
from address import AddressParser, Address
from nameparser import HumanName
import xlrd
import csv
class Category:
name = ""
contents = []
index = 0
columns = []
alltext = ""
with open('test.csv', 'rb') as csvfile:
document = csv.reader(csvfile, delimiter=',', quotechar='\"')
for row in document:
alltext = alltext + ', '.join(row) + "\n"
splitText = alltext.split('\n')
categoryNames = splitText[0].split(', ')
ixt = 0
for name in categoryNames:
thisCategory = Category()
thisCategory.name = name
thisCategory.index = ixt
columns.append(thisCategory)
ixt = ixt + 1
for line in splitText:
if(line != splitText[0] and len(line) != 0):
individualItems = line.split(', ')
for index, item in enumerate(individualItems):
if(columns[index].index == index):
print(item + " (" + str(index) + ") is being sent to " + columns[index].name)
columns[index].contents.append(item)
for col in columns:
print("-----" + col.name + " (" + str(col.index) + ")-----")
for stuff in col.contents:
print(stuff)
当代码运行时,它会为每个项目提供一个输出:
Bob (0) is being sent to First Name
Robertson(1) is being sent to Last Name
这是它应该做的。每个项目都说它被发送到正确的类别。然而,最后,不是让每个项目都在它声称的类别中,而是每个类别都有每个项目,而不是这样:
-----First Name-----
Bob
Roberta
Leo
Carter
Mats
Lucas
对于每个类别,依此类推。我明白了:
-----First Name-----
Bob
Robertson
Roberta
Robertson
123 South Street
Salt Lake City
UT
Leo
Smart
Carter
Smart
827 Cherry Street
Macon
GA
Mats
Lindgren
Lucas
Lindgren
237 strawberry xing
houston
tx
我不知道出了什么问题。这两行代码之间没有任何东西可能会搞砸。
【问题讨论】:
-
您可以学习How to Ask a good question并创建Minimal, Complete, and Verifiable示例。这使我们更容易为您提供帮助。具体这里没有数据,而且有相当多的代码似乎与问题无关。
-
我已经编辑了我的问题。希望它现在更符合标准。