Tag: python 2.7

如何将excel文件中的特定数据附加到python列表中?

我有导入一个Excel文件。 excel文件有这样的2行5列: Weights 1 5 9 8 Criteria Number 38 89 8 56 excel_file = tkFileDialog.askopenfilename(filetypes=[('excelfile','*.xlsx')],title='Choose a .xlsx file') n_crit = [] workbook = xlrd.open_workbook(excel_file) sheet = workbook.sheet_by_index(0) data = [] for r in range(sheet.nrows): sublist = [] for c in range(sheet.ncols): if r == "Weights": sublist.append(sheet.cell_value(r,c)) data.append(sublist) print data 我想追加到Excel文件中的数据列表数据。 如果任何列中的第一个单元格是Weights,那么它会将除了第一列值(权重)之外的所有权重行中的数字附加到数据列表中: data = [[1 […]

如何在Python中将n行xlsx转换为csv,同时保留date值

我试图将xlsx文件转换为一个CSV文件,其中包含标题和另一个包含实际数据的CSV文件。 我有以下要求: 标题不是从第一行开始,而是在start_line行。 date不应该被视为浮动,但在一些string格式。 事先不知道文件的行数或列数。 我也不想指定哪一列是date。 使用pandas我卡在1号。我想在两个单独的读取,我从start_line读start_line+1和从start_line+1到结束读取。 但是,似乎不可能从偏移量中读取n行。 下面是我用来获取一个包含头文件的代码。 import pandas as pd def parse_excel(file,start_line,sheet,table): sh = pd.read_excel(file,sheet,skiprows=start_line) sh.to_csv("output.csv",sep='\t',encoding='utf-8',index=False) 接下来,我已经尝试使用xlrd但是这个库将所有date视为像在Excel中的浮动。 唯一的解决方法似乎是通过所有单独的单元格 ,这似乎不是很有效或编码不好。 我现在拥有的: import xlrd def parse_excel(file,start_line,sheet,table): with xlrd.open_workbook(file) as wb: sh = wb.sheet_by_name(sheet) header_written = False with open('{0}.csv'.format(table),'wb') as csv_file: wr = csv.writer(csv_file,delimiter='\t') for rownum in range(sh.nrows): if not header_written and start_line == rownum: […]

如何search一个string的CSV文件,并得到它的行索引在Python 2.7

我正在尝试打开一个csv并search第一列的电子邮件。 如果存在,则检查该特定行的第三列是否为“1”。 我无法弄清楚如何只检查其中的电子邮件行的第三列。 有没有一种方法,我可以得到该行的索引,电子邮件是在? 那么我可以做一些像row [Row#] [2]的东西,所以它会search该行的第3列的电子邮件。 这是我现在所拥有的: with open('MyFile.csv', 'rb') as f: reader = list(csv.reader(f)) for row in reader: if Email in row[0]: #checks if email exist if '1' in row[2]: #searches every row instead of just the one with email print "Account Already = 1" 我怎样才能添加这样的东西: with open('MyFile.csv', 'rb') as f: reader = […]

不明白这个错误意味着什么:TypeError:'int'对象不可调用

我是新来的python和努力了解这个错误意味着什么:TypeError:'int'对象不可调用 这里是完整的代码: import openpyxl, pprint,os os.chdir('C:\Users\Shend_000\Documents\Py CWD') wb=openpyxl.load_workbook('censuspopdata.xlsx') sheet=wb.get_sheet_by_name('Population by Census Tract') countyData={} for row in range(2,sheet.max_row()): state=sheet['B'+str(row)].value county=sheet['C'+str(row)].value pop=sheet['D'+str(row)].value countyData.setdefault(state,{}) countyData[state].setdefault(county,{'tracts':0, 'pop':0}) countyData[state][county]['tracts']+=1 countyData[state][county]['pop']+=int(pop) resultFile=open('census2010.py','w') resultFile.write('allData= '+pprint.pformat(countyData)) resultFile.close() print 'Done.'

openpyxl – 读取循环中的下一个单元格值

我正在创build一个循环遍历表中的单元格。 在某些情况下,我想要访问循环中的下一个单元格值。 import openpyxl wb = openpyxl.load_workbook('test.xlsx') wb = wb.get_sheet_by_name("Sheet 1") for row in wb['A1' : 'B150']: for cell in row: print cell.value # prints current active cell if something: # print next cell value 请build议一种方法来访问循环中的下一个单元格值。 我正在使用Python 2.7。 提前致谢。

显示AttributeError:'unicode'对象在xlrd python中没有属性'nrows'?

我正在使用xlrd模块读取python中的excel文件。 示例代码如下 import xlrd import sys import glob workbook = xlrd.open_workbook('nvm_sw_cfg_parm_master_v85.xlsx') sheet_names = workbook.sheet_names() print('Sheet Names', sheet_names) for sheet in sheet_names: print sheet print sheet.nrows 这是我想要的示例代码。 在这个代码中,我可以读取工作表名称,但是当我读取工作表中的总行数时,它显示错误 AttributeError: 'unicode' object has no attribute 'nrows' 我已经通过库文件文档里面有一个类名称调用表里面的nrows被定义,我怎么可以从代码调用这个,请问有人可以帮我解决这个错误或任何其他模块读取excel文件。

将带有特殊字符的Excel工作表输出为CSV

我有一个Excel文件与多个工作表。 我想从文件中获取一个特定的工作表,并将其保存为一个CSV文件,用逗号分隔(我还没有得到这个)。 工作表中包含特殊字符的数据与右下方的最后一列相似。 我不介意忽略这些字符。 **DateStamp Country ComputerName Domain IPAddress OperatingSystem** 2017-05-24 USA Computer1 Domain1 1.2.3.4 Windows 2008 2017-05-24 England Computer2 Domain2 1.2.3.5 Windows Server® 2008 我的代码到目前为止是: import os import xlrd import sys file = 'path/to/my/file.xlsx' workbook = xlrd.open_workbook(file) sheet = workbook.sheet_by_name('Data') for rowx in range(sheet.nrows): coldata = sheet.row_values(rowx) coldata = " ".join(str(x) for x in […]

如何忽略使用python xlsxwriter生成的.xlsx文件中显示的“Inconsistent Formula”警告?

在我的文件中有连续的MAX和MIN公式。 样品 CELLS – | A | B | C | D | E | F | G | H | 行:| MAX | MIN | MIN | MAX | MIN | MIN | MAX | MIN | MIN 如果打开Excel表格,则会显示一个绿色三angular形,并显示警告消息“不一致的公式”。

如何在Python中交叉引用两个字典。 所以他们可以打印一个不错的excel表格

所以我现在有这样的字典设置: Dict = {'customer1': {datetime.datetime(2017, 6, 15, 14, 34, 55): '12', datetime.datetime(2017, 6, 16, 14, 34, 55): '14'}, 'customer2': {datetime.datetime(2017, 6, 16, 14, 34, 55): '16', datetime.datetime(2017, 6, 17, 14, 34, 55): '18'}} 我想打印的是这样的: ——— 15/06/2017 16/06/2017 17/06/2017 customer1 12 14 — customer2 16 18 我想过创造另一个温度。 字典。 先按datesorting,然后按顾客名称sorting,然后交叉引用这两个清单。 我确实设法创造另一个字典。 看起来像这样: tempDict = {datetime.datetime(2017, 6, 15, […]

XLWT:如何访问以前写入的单元格?

我已经使用'xlwt'创build了一个excel工作簿。在我的程序中,下面给出了几个单元格的一些值。 现在,我如何访问这些单元格? """ Create 'Data_Table.xls' Excel workbook """ book = xlwt.Workbook() sheet1 = book.add_sheet("Data_Table") """ Define First row in 'Data_Table.xls'""" sheet1.write(0,0, "Variable Name") sheet1.write(0,1, "Global Static Variable?") 我如何访问单元格(0,0)或(0,1)中的数据?