【发布时间】:2017-04-19 15:24:14
【问题描述】:
我有两种文件,excel 和 csv,我用它们来读取具有两个永久列的数据:问题、答案和两个临时列,它们可能存在也可能不存在 Word 和 Replacement。
我已经制作了不同的函数来从 csv 和 excel 文件中读取数据,这些文件将根据文件的扩展名被调用。
有没有办法根据临时列(Word 和 Replacement)的存在时间和不存在时间来读取临时列(Word 和 Replacement)中的数据。请看下面的函数定义:
1) 对于 CSV 文件:
def read_csv_file(path):
quesData = []
ansData = []
asciiIgnoreQues = []
qWithoutPunctuation = []
colnames = ['Question','Answer']
data = pandas.read_csv(path, names = colnames)
quesData = data.Question.tolist()
ansData = data.Answer.tolist()
qWithoutPunctuation = quesData
qWithoutPunctuation = [''.join(c for c in s if c not in string.punctuation) for s in qWithoutPunctuation]
for x in qWithoutPunctuation:
asciiIgnoreQues.append(x.encode('ascii','ignore'))
return asciiIgnoreQues, ansData, quesData
2) 读取excel数据的函数:
def read_excel_file(path):
book = open_workbook(path)
sheet = book.sheet_by_index(0)
quesData = []
ansData = []
asciiIgnoreQues = []
qWithoutPunctuation = []
for row in range(1, sheet.nrows):
quesData.append(sheet.cell(row,0).value)
ansData.append(sheet.cell(row,1).value)
qWithoutPunctuation = quesData
qWithoutPunctuation = [''.join(c for c in s if c not in string.punctuation) for s in qWithoutPunctuation]
for x in qWithoutPunctuation:
asciiIgnoreQues.append(x.encode('ascii','ignore'))
return asciiIgnoreQues, ansData, quesData
【问题讨论】:
-
您考虑过
pandas.read_csv和pandas.read_excel吗?它们将根据存在的列自动读取。 -
@tmrlvi,我在读取 csv 函数时使用了 pandas.read_csv,但列标题必须在 colnames 中提供。但是如果我没有单词和替换 cloumns 怎么办?
-
您不必提供它们。如果您不提供,
pandas推断名称。还是您的数据不包含标头? -
我的数据包含标题(问题、答案、单词、替换)。所以你是说如果我不在代码中提供 colnames,熊猫将从第二行读取?
-
无论如何它从第二行读取,除非您提供
header=None