从网上我发现WSGI是一个用于python web开发/框架的CGI.FCGI似乎是各种语言的更通用的网关.在参考python和C/++语言时,不知道两者之间的性能差异.
如何用python删除文件的最后一行?
输入文件示例:
hello
world
foo
bar
Run Code Online (Sandbox Code Playgroud)
输出文件示例:
hello
world
foo
Run Code Online (Sandbox Code Playgroud)
我创建了以下代码来查找文件中的行数 - 但我不知道如何删除特定的行号.我是python的新手 - 所以如果有更简单的方法 - 请告诉我.
try:
file = open("file")
except IOError:
print "Failed to read file."
countLines = len(file.readlines())
Run Code Online (Sandbox Code Playgroud)
编辑:
我想出了各种各样的答案:主要是草莓和我在网上看到的东西(对不起,我找不到链接).
#!/usr/bin/env python
import os, sys
readFile = open("file")
lines = readFile.readlines()
readFile.close()
w = open("file",'w')
w.writelines([item for item in lines[:-1]])
w.close()
Run Code Online (Sandbox Code Playgroud) 现在我正在使用各种正则表达式将mediawiki标记中的数据"解析"为列表/词典,以便可以使用文章中的元素.
这不是最好的方法,因为必须进行的案例数量很大.
如何将文章的mediawiki标记解析为各种python对象,以便可以使用其中的数据?
示例是:
各种正则表达式可以实现上述目标,但我发现我必须做出相当大的数字.
这是mediawiki非官方规范(我发现它们的官方规范没有用).
#!/usr/bin/python
import random
import string
appendToFile = open("appendedFile", "a" )
# Generator
for i in range(1, 100000):
chars = "".join( [random.choice(string.letters) for i in xrange(15)] )
chars2 = "".join( [random.choice(string.letters) for i in xrange(15)] )
appendToFile.write(chars + ":" + chars2 + "\n")
appendToFile.close()
Run Code Online (Sandbox Code Playgroud)
从这个问题修改的代码.
上面的代码以STRING:STRING的格式生成100,000行随机文本.结果文本文件是3.1 MB.
如何使用STRING中的第一个STRING快速将文件字母化:STRING?案件无关紧要.
冒泡排序很慢,不是吗?
我有这个xpath查询:
/html/body//tbody/tr[*]/td[*]/a[@title]/@href
Run Code Online (Sandbox Code Playgroud)
它使用title属性提取所有链接 - 并href在FireFox的Xpath检查程序附加组件中提供.
但是,我似乎无法使用它lxml.
from lxml import etree
parsedPage = etree.HTML(page) # Create parse tree from valid page.
# Xpath query
hyperlinks = parsedPage.xpath("/html/body//tbody/tr[*]/td[*]/a[@title]/@href")
for x in hyperlinks:
print x # Print links in <a> tags, containing the title attribute
Run Code Online (Sandbox Code Playgroud)
这不会产生lxml(空列表)的结果.
如何在Python下抓取href包含属性标题的超链接的文本(链接)lxml?
我创建了一个python脚本,每次按下键盘上的Super(或WinKey)时都需要执行一个事件.
如果没有python进程"聚焦",如何实现这一点 - 因为它在后台运行,等待按键执行事件?
我在网上看到很多帖子向我展示了如何阅读输入 - 但他们都需要一个让这个过程"专注",而且没有人向我展示如何使用python脚本捕获Super(或WinKey).
我正在运行Ubuntu 9.10.
资料来源:美国独立宣言
如何将上述源文本拆分为多个子字符串,包含"n"个字?
我使用split('')来提取每个单词,但是我不知道如何在一个操作中使用多个单词.
我可以浏览我所拥有的单词列表,并通过将第一个列表中的单词粘合在一起来创建另一个单词(同时添加空格).然而,我的方法不是非常pythonic.
资源:
[This] is some text with [some [blocks that are nested [in a [variety] of ways]]]
Run Code Online (Sandbox Code Playgroud)
结果文字:
[This] is some text with
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法可以做到这一点 - >或者必须达到pyparsing(或其他解析库)?
#!/usr/bin/env python
# Display window with toDisplayText and timeOut of the window.
from Tkinter import *
def showNotification(notificationTimeout, textToDisplay):
## Create main window
root = Tk()
Button(root, text=textToDisplay, activebackground="white", bg="white", command=lambda: root.destroy()).pack(side=LEFT)
root.update_idletasks()
# Remove window decorations
root.overrideredirect(1)
timeOut = int(notificationTimeout*1000) # Convert to ms from s
## Run appliction
root.after(timeOut,root.destroy)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
上面的代码创建了一个带有提示的通知.但是在Windows上 - 通知不会自动弹出所有其他当前窗口.必须单击kill按钮(文本),并在第一次对焦,之后根窗口将显示在所有其他窗口上方.
有没有办法让通知自动出现在所有其他窗口之上 - 在Windows上?
它似乎在Linux上工作得很好(ubuntu 9.10).
从目录列表中转储的示例:
hello:3.1 GB
world:1.2 MB
foo:956.2 KB
Run Code Online (Sandbox Code Playgroud)
以上列表的格式为FILE:VALUE UNIT.如何根据文件大小订购上面的每一行?
我想也许可以通过模式":VALUE UNIT"(或以某种方式使用分隔符)解析单元的每一行,然后通过ConvertAll引擎运行它,从字节中接收每个值的大小,用其余的行哈希(文件名),然后通过大小对结果字典对进行排序.
麻烦的是,我不知道模式匹配.但是我看到你可以对字典进行排序
如果有更好的方向来解决这个问题,请告诉我.
编辑:
我的列表实际上是在一个文件中.从(令人敬畏的)Alex Martelli的答案中获取灵感,我写了以下代码,从一个文件中提取,命令并写入另一个文件.
#!/usr/bin/env python
sourceFile = open("SOURCE_FILE_HERE", "r")
allLines = sourceFile.readlines()
sourceFile.close()
print "Reading the entire file into a list."
cleanLines = []
for line in allLines:
cleanLines.append(line.rstrip())
mult = dict(KB=2**10, MB=2**20, GB=2**30)
def getsize(aline):
fn, size = aline.split(':', 1)
value, unit = size.split(' ')
multiplier = mult[unit]
return float(value) * multiplier
print "Writing sorted list to file."
cleanLines.sort(key=getsize)
writeLines …Run Code Online (Sandbox Code Playgroud) 列表的示例列表:
[
["url","name","date","category"]
["hello","world","2010","one category"]
["foo","bar","2010","another category"]
["asdfasdf","adfasdf","2010","one category"]
["qwer","req","2010","another category"]
]
Run Code Online (Sandbox Code Playgroud)
我想做的是创建一个字典 - >类别:[条目列表].
结果字典将是:
{"category" : [["url","name","date","category"]],
"one category" : [["hello","world","2010","one category"],["asdfasdf","adfasdf","2010","one category"]],
"another category" : [["foo","bar","2010","another category"], ["qwer","req","2010","another category"]]}
Run Code Online (Sandbox Code Playgroud) python ×11
dictionary ×2
extraction ×2
alphabetical ×1
api ×1
arrays ×1
binding ×1
brackets ×1
daemon ×1
fastcgi ×1
file ×1
focus ×1
hash ×1
hyperlink ×1
keypress ×1
line ×1
list ×1
lxml ×1
map ×1
mediawiki ×1
nested ×1
parsing ×1
recursion ×1
regex ×1
sorting ×1
split ×1
stack ×1
string ×1
substring ×1
text ×1
tkinter ×1
ubuntu-9.10 ×1
windows ×1
words ×1
wsgi ×1