小编Bir*_*irt的帖子

如何提高python中这个readline循环的速度?

我正在将文本格式的Databasedump的几个部分导入MySQL,问题是在有趣的数据之前,非常有趣的东西面前.我写了这个循环来获取所需的数据:

def readloop(DBFILE):
    txtdb=open(DBFILE, 'r')

sline = ""

# loop till 1st "customernum:" is found
while sline.startswith("customernum:  ") is False: 
    sline = txtdb.readline()

while sline.startswith("customernum:  "):
    data = []
    data.append(sline)
    sline = txtdb.readline()
    while sline.startswith("customernum:  ") is False:
        data.append(sline)
        sline = txtdb.readline()
        if len(sline) == 0:
            break
    customernum = getitem(data, "customernum:  ")
    street = getitem(data, "street:  ")
    country = getitem(data, "country:  ")
    zip = getitem(data, "zip:  ")
Run Code Online (Sandbox Code Playgroud)

Textfile非常庞大,所以只需循环直到第一个想要的条目需要花费很多时间.任何人都有一个想法,如果这可以更快地完成(或者如果整个方式我修复这不是最好的主意)?

提前谢谢了!

python loops readline

2
推荐指数
1
解决办法
2854
查看次数

尝试使用GData API将图片从Google App Engine上传到Picasa时出现TypeError

我正在尝试编写一个小工具,将图片从Google App Engine上传到Picasa.获取图像有效,但是当我尝试上传它时,我得到错误" TypeError:stat()参数1必须是(没有NULL字节的编码字符串),而不是str "

该代码基本上如下所示:

def getfile(url):
    result = urlfetch.fetch(url)
    if result.status_code == 200:
        return (result.content)
    logging.error ("[-] Error fetching URL: %s" % url)

def uploadpicture(comment,pic):
    album_url = '/data/feed/api/user/%s/album/%s' % (username, album)
    fname = "image.jpg"
    entry = gd_client.InsertPhotoSimple(album_url, fname, comment, pic, content_type='image/jpeg')

picurl = "http://brilliantleap.com/blog/frog.jpg"
pic = getfile(picurl)
comment = "Test"
uploadpicture(comment, pic)
Run Code Online (Sandbox Code Playgroud)

完整的Stacktrace是:

Traceback(最近一次调用最后一次):

调用 handler.get(*groups)中输入文件"/ home/birt/stuff/google/appengine/ext/webapp/init .py",第507行

文件"/home/birt/stuff/app_picasaupload/main.py",第124行,获取uploadpicture(评论,pic)

在uploadpicture entry = gd_client.InsertPhotoSimple(album_url,fname,comment,pic,content_type ='image/jpeg')中输入文件"/home/birt/stuff/app_picasaupload/main.py",第104行

在InsertPhotoSimple content_type中输入文件"/home/birt/stuff/app_picasaupload/gdata/photos/service.py",第469行

在InsertPhoto中输入文件"/home/birt/stuff/app_picasaupload/gdata/photos/service.py",第398行os.path.exists(filename_or_handle):#这是一个文件名

文件"/usr/lib/python2.5/posixpath.py",第171行,存在st = os.stat(path)

文件"/home/birt/stuff/google/appengine/tools/dev_appserver.py",1109,在通话 如果不是FakeFile.IsFileAccessible(路径):

文件"/home/birt/stuff/google/appengine/tools/dev_appserver.py",第1018行,IsFileAccessible normcase = …

python google-app-engine picasa gdata-api typeerror

2
推荐指数
1
解决办法
2944
查看次数