Tag: python

使用Python处理CSV中的新行字符

我试图上传一个CSV文件到一个SQL Server数据库使用python.I无法处理新行字符。该文件行为不同在MS Excel和Noptepad ++ 以下是包含新行字符的CSV文件的示例。该文件在记事本++中看起来像这样 但在excel中看起来像这样 。 文本分成C栏两部分 我试图处理这样的换行符 wfile = open(UploadFile, "rU") reader = csv.reader(wfile,delimiter = ",",dialect='excel') with open(UploadFile, "r") as uploadData: formatter_string = "%d/%m/%y %H:%M" for row in reader: datetime_object = datetime.strptime(row[9], formatter_string) row[9] = str(datetime_object.date()) cursor.execute("insert into "+UploadTable+" values ("+(row[9])+","+(row[0])+","+(row[1])+","+(row[2])+","+(row[8])+","+(row[3])+","+(row[4])+","+(row[5])+","+(row[6])+","+(row[7])+")") 我在这里阅读这个当我试图上传这个文件时,我得到了这个错误 Failed to upload Facebook data. list index out of range 我不确定发生了什么问题。 […]

从字典键中定义Excel单元格名称

我正在尝试input数据到一系列命名的Excel工作表,例如CellName1 ,而不是A1 ,基于字典中的一个键。 我正在使用的代码如下所示: snCells = {} for item1 in defVars: if item1.find("LFDef"): ws = wLF snCells.update({item1:ws.Range[item1](1,1)}) elif item1.find("UFDef"): ws = wLF snCells.update({item1:ws.Range["%s" % item1](1,1)}) else: ws = wST snCells.update({item1:ws.Range["%s"% item1](1,1)}) 哪里: ex = Marshal.GetActiveObject("Excel.Application") wb = ex.ActiveWorkbook ws = wb.ActiveSheet wUF = wb.Sheets(1).Select wLF = wb.Sheets(2).Select wST = wb.Sheets(4).Select 范围应该定义Python在Excel中select的单元格,但是我不断收到错误,说该范围没有属性。 有没有解决这个错误的方法?

Python 2.7:我无法使用xlwt.Workbook._Workbook__worksheets删除工作表

我有一个包含12张工作表的工作簿。 我想创build一个新的文件删除他们7。 但是我不能摆脱“旧文件”,“Licencias”和“HTM”。 这是一个工作簿的示例,这里是我的代码。 请帮我看看我做错了什么。 提前致谢。 import xlrd, xlwt, xlutils.copy #open the report, assign it to a variable reporteWilly = xlrd.open_workbook('cctable.xls') #create a writable copy of the report. reporteWillyBk = xlutils.copy.copy(reporteWilly) #Delete the sheets I dont want in the file #by accessing the internal of the object xlwt.Workbook._Workbook_worksheets #and there I'll specify only the elements […]

在Excel中通过xlwt for pythonsorting多列

我正在使用python写一个报告放入excel涂层。 有四个栏目,分别是: 产品名称| 前值| 当前值| 区别 当我把所有的价值完成后,我想根据当前价值进行sorting。 有没有办法在xlwt中做到这一点? 我只看到了sorting单个列的例子。

将DataFrame(长格式数据)重塑为“摘要”DataFrame

所有,我是新的python。 我试图自动化重塑,我通常在Excel中执行如下 #Import long form data from CSV into pandas data frame In 1: Data = [My Long Form DataFrame imported from a CSV file] City Population Chicago 1,245 Los Angeles 2,457 New York 998 Chicago 1,854 New York 1,654 Los Angeles 2,478 Los Angeles 760 more… #Reshape "Data" DataFrame to a summary DataFrame "DataByCity" […]

通过Python中的win32过滤Excel列

是否有可能通过在Python中的Win32过滤excel列? 谢谢

读取使用python在Excel中合并的列/行

我无法从合并了行和列的电子表格中读取信息。 我试过使用merged_cell来获得价值,但我不明白它是如何工作的。 谁能帮我? 使用下面的脚本,我可以得到的值, 除了合并单元格。 我怎么能得到所有的单元格,包括合并的单元格? from xlrd import open_workbook,cellname workbook = open_workbook('file.xls') sheet = workbook.sheet_by_index(0) for row_index in range(sheet.nrows): for col_index in range(sheet.ncols): print cellname(row_index,col_index),'-', print sheet.cell_value(row_index,col_index)

使用python读取excel中的指定列

所以我需要使用python输出一个列名下的所有东西。 到目前为止,我可以阅读整个Excel文件。 在我的工作簿中,我有很多数据表,所以我指定了哪一个打开,现在我只需要输出指定列下的所有东西。 这是我的代码 import xlrd file_Location = "/home/jerad/Desktop/Register.xlsx" workbook = xlrd.open_workbook(file_Location) sheet = workbook.sheet_by_name('CM4000 Register View') num_rows = sheet.nrows – 1 curr_row = -1 while curr_row < num_rows: curr_row += 1 row = sheet.row(curr_row) #this is print everything in the file. print row

编码错误 – xlsxwriter – Python

我试图从一个文件拆分行,并把它们放入一个Excel文件(xlsx)。 根据PS PAD,文件的编码是'cp1250'。 所以要在xlsx文件中有正确的字符,我从cp1250 – line = line.decode("cp1250")解码这行 问题是从12000 cca 3000行返回这个错误: 'charmap' codec can't decode byte 0x81 in position 25: character maps to <undefined> 所以我试图解码的下一个东西(“UTF-8”),我不知道为什么,但它更好。 只有330行返回错误: 'utf8' codec can't decode byte 0x8e in position 0: invalid start byte 你们有什么想法我做错了吗? 编辑:错误主要发生当行包含'Ž'或'Š' 这里是代码:(在py文件的顶部我已经把“# – – coding:utf-8 – – ”) def toXls(file): workbook = xlsxwriter.Workbook(file) worksheet = workbook.add_worksheet() […]

Python:当文件在列标题中包含特殊字符时,使用Pandas读取Excel文件

我知道你可以用pandas阅读Excel文件,但是在工作表中的列标题没有像纯文本那样易于阅读的格式的文件中,我遇到了麻烦。 换句话说,如果列标题有特殊字符,那么文件将无法导入。 就像您将数据导入到Microsoft Access或其他数据库一样,您可以select导入或删除特殊字符。 我唯一的解决办法是编写一个Excelmacros来去除导入时通常不被数据库喜欢的字符,然后使用python导入文件。 但是,必须有一种纯粹使用python来处理这种情况的方法(这个速度要快得多)。 我的问题是,如果python处理导入.xls和.xlsx文件,当列标题有特殊字符不会导入?