Tag: python2.7的

python csv将所有行格式化为一行

我是一个csv文件,我想获得一列中的所有行。 我试图导入到MS Excel或格式与Notedpad ++。 但是,每次尝试都会将一段数据视为新行。 如何使用pythons csv模块格式化文件,以便删除string“BRAS”并更正格式。 每一行都在一个引号和分隔符之间被find|更新: "aa|bb|cc|dd| ee|ff" "ba|bc|bd|be| bf" "ca|cb|cd| ce|cf" 以上是3行,但我的编辑看到他们5行或6等等。 import csv import fileinput with open('ventoya.csv') as f, open('ventoya2.csv', 'w') as w: for line in f: if 'BRAS' not in line: w.write(line) NB当我尝试在python中使用时遇到unicode错误。 return codecs.charmap_decode(input,self.errors,decoding_table)[0] UnicodeDecodeError: 'charmap' codec can't decode byte 0x8f in position 18: character maps to <undefined>

在'For'循环中调用一个函数会引发一个NameError

我试图从FOR循环中调用一个函数,但得到的错误: test() NameError: name 'test' is not defined 代码如下: from selenium import webdriver from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.keys import Keys import time from lxml import html import requests import xlwt browser = webdriver.Firefox() # Get local session of firefox # 0 wait until the pages are loaded browser.implicitly_wait(3) # 3 secs should be enough. […]

如何在django中以views.py的excel选项创build下载

我想添加我的Django应用程序的views.py选项“下载为Excel”。 HttpResponse在html文件中。 这几个views.py看起来像这样 def auto_run_html(request): htmlfile = HttpResponse() Month_no = request.GET['Month'] htmlfile.write('<html><HEAD><LINK href="/static/timesheet.css" rel="stylesheet" type="text/css"></HEAD>') htmlfile.write('<body>') htmlfile.write('''<table class = "Emp_Details" > <tr style="text-align:left"> <th>{}</th><th>{}</th> </tr> <tr style="text-align:left"> <th>{}</th><th>{}</th> </tr> <tr style="text-align:left"> <th>{}</th><th>{}</th> </tr> </table>'''.format('Department',dept_row,'Employee ID',dept_row[2],'Employee Name',dept_row)) return HttpResponse(htmlfile) 在这里我想添加下载作为excel操作。 我试过这个输出csv,但它只是为了显示为csv文件,而不是浏览器中的html。告诉用户可以保存为csv文件的文档,但它只能提供html页面的选项。 我正在使用django 1.6&python 2.7。 这可能是重复的。 我是新来的python,如果它只能由python-excel程序完成,那么请举例参考我的上述代码。 预先感谢。 这是我的csv编码veiws.py.Please标记我在哪里做错了。 def auto_run_html(request): response = HttpResponse(content_type='text/csv') response['Content-Disposition'] = 'attachment; […]

MySQL不接受executemany()INSERT,从Excel运行Python(datanitro)

我已经增加了自己的答案,但工作,但改善开放 在datanitro上看到一个项目后。 我接触到MySQL的连接(他们使用SQLite),我能够从MySQL中导入一个小的testing表到Excel中。 从Excel工作表中插入新的更新数据是下一个任务,到目前为止,我可以得到一行工作就像这样… import MySQLdb db = MySQLdb.connect("xxx","xxx","xxx","xxx") c = db.cursor() c.execute("""INSERT INTO users (id, username, password, userid, fname, lname) VALUES (%s, %s, %s, %s, %s, %s);""", (Cell(5,1).value,Cell(5,2).value,Cell(5,3).value,Cell(5,4).value,Cell(5,5).value,Cell(5,6).value,)) db.commit() db.close() …但在多行尝试将失败。 我怀疑在遍历Excel中的行时出现问题。 这是我到目前为止… import MySQLdb db = MySQLdb.connect(host="xxx.com", user="xxx", passwd="xxx", db="xxx") c = db.cursor() c.execute("select * from users") usersss = c.fetchall() updates = [] […]

为什么zipfile.is_zipfile在xlsx文件上返回True?

我正在使用is_zipfile在提取之前检查它是否为is_zipfile 。 但是该方法从一个StringIO对象的Excel文件返回True。 我正在使用Python 2.7。 有谁知道如何解决这一问题? 使用is_zipfiile可靠吗? 谢谢。