我是python的新手,我在插座上试试运气.所以我写了一个简单的http客户端,但令我惊讶的是它无法访问firefox可以访问的网页,但他们使用相同的标题
import socket
clientsocket= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(("213.229.83.205",80))#connect to proxy at given address
print "connected to 213.229.83.205"
sdata= """GET http://google.co.ug/ HTTP/1.1
Host: google.co.ug
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:6.0) Gecko/20100101 Firefox/6.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Proxy-Connection: keep-alive
Cookie: cookie <-- Real cookie deleted
"""
print "sending request"
clientsocket.send(sdata);
rdata=clientsocket.recv(10240)
if not rdata: print "no data found"
else:
print "receiving data !"
myfile=open("c:/users/markdenis/desktop/google.html","w")
myfile.write(str(rdata))
myfile.close()
print "data written to file on desktop"
clientsocket.close()
raw_input()#system(pause)
Run Code Online (Sandbox Code Playgroud)
当我运行它时,它显示:
connected to 213.229.83.205
sending …Run Code Online (Sandbox Code Playgroud) 我正在学习Django的一些教程,用于即将开展的项目,我无法正确加载模板.调试模式返回"ValueError",表示"需要多于1个值才能解压缩".我正在运行Django的捆绑服务器.知道问题是什么?任何帮助表示赞赏.
这是跟踪:
Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
111. response = callback(request, *callback_args, **callback_kwargs)
File "/Users/tinwaijosephlee/Sites/djcode/dev2/../dev2/views.py" in hours_ahead
26. return render_to_response('plus.html', {'offset': offset, 'dt': dt})
File "/Library/Python/2.7/site-packages/django/shortcuts/__init__.py" in render_to_response
20. return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/Library/Python/2.7/site-packages/django/template/loader.py" in render_to_string
181. t = get_template(template_name)
File "/Library/Python/2.7/site-packages/django/template/loader.py" in get_template
157. template, origin = find_template(template_name)
File "/Library/Python/2.7/site-packages/django/template/loader.py" in find_template
128. loader = find_template_loader(loader_name)
File "/Library/Python/2.7/site-packages/django/template/loader.py" in find_template_loader
93. module, attr = loader.rsplit('.', 1)
Run Code Online (Sandbox Code Playgroud)
这是我的观看代码:
from django.shortcuts import render_to_response
from django.http import …Run Code Online (Sandbox Code Playgroud) 我正试图通过练习11艰难之路学习Python并遇到一些问题.下面是我使用geedit在.py文件中输入的内容(在PC上处理)
print "How old are you?",
age = raw_input('27')
print "How tall are you?",
height = raw_input('5\'8"')
print "How much do you weigh?",
weight = raw_input('180lbs')
print "So, you're %r old, %r tall and %r heavy." %(age, height, weight)
Run Code Online (Sandbox Code Playgroud)
我不能让%r显示原始输入,他们倾向于在最后一行出现我做错了什么?
我正在寻找重构下面的Python代码的最佳方法.我认为在2或3行代码中有一种Pythonic方法可以做到这一点,但无法弄明白.我搜索过Stackoverflow但找不到类似的问题和解决方案.非常感谢!
list1 = [(Python, 5), (Ruby, 10), (Java, 15), (C++, 20)]
list2 = [(Python, 1), (Ruby, 2), (Java, 3), (PHP, 4), (Javascript, 5)]
# I want to make an unsorted list3 like this
# list3 = [(Python, 6), (Ruby, 12), (Java, 18), (PHP, 4), (Javasript, 5), (C++, 20)]
common_keys = list(set(dict(list1).keys()) & set(dict(list2).keys()))
if common_keys:
common_lst = [(x, (dict(list1)[x] + dict(list2)[x])) for x in common_keys]
rest_list1 = [(x, dict(list1)[x]) for x in dict(list1).keys() if x not in common_keys]
rest_list2 = …Run Code Online (Sandbox Code Playgroud) 尝试在文本文件的内容上使用通配符在python中进行搜索/替换:
如果文本文件的内容如下所示:
“ all_bcar_v0038.ma”; “ all_bcar_v0002.ma”; “ all_bcar_v0011.ma”; “ all_bcar_v0011.ma”;
希望将所有版本号替换为v1000以获得此信息:
“ all_bcar_v1000.ma”; “ all_bcar_v1000.ma”; “ all_bcar_v1000.ma”; “ all_bcar_v1000.ma”;
并把文件写出来。
我在下面尝试过,但是发生的是该脚本仅捕获第一个版本号,而其他版本未受影响:
def replaceAll(file,searchExp,replaceExp):
for line in fileinput.input(file, inplace=1):
if searchExp in line:
line = line.replace(searchExp,replaceExp)
sys.stdout.write(line)
rigs = ['all_bcar']
rigs_latest = ['all_bcar_v1000']
old_pattern = []
old_compiled = []
old = []
old_version = []
for rig in range(len(rigs)):
old_pattern.append("/" + rigs[rig] + "_(.*).ma")
fin = open(txt_file, "r")
old_compiled.append(re.compile(old_pattern[rig]))
old.append(old_compiled[rig].search(fin.read()))
old_version.append(old[rig].group(1).strip())
old_rig = (rigs[rig] + "_" + old_version[rig])
replaceAll(txt_file,old_rig,rigs_latest[rig])
fin.close()
Run Code Online (Sandbox Code Playgroud)
不确定如何保持搜索循环以查找其他版本并避免已被替换的版本,以跳过任何等于“ …
我不知道如何做到这一点:我有一个list的list这样定义S:
list=[[day,type,expense],[...]];
Run Code Online (Sandbox Code Playgroud)
日和费用是int,类型是string
我需要在白天找到最大费用.一个例子:
list=[[1,'food',15],[4,'rent', 50],[1,'other',60],[8,'bills',40]]
Run Code Online (Sandbox Code Playgroud)
我需要总结当天的元素并找到费用最高的那一天.
结果应该是:
day:1, total expenses:75
我有一个Django应用程序,允许用户创建变量并命名它们
class Product(models.Model):
name = models.CharField(max_length=40, unique=True)
int1_name = models.CharField(max_length=60, blank=True, null=True)
int1_default = models.IntegerField(blank=True, null=True)
int2_name = models.CharField(max_length=60, blank=True, null=True)
int2_default = models.IntegerField(blank=True, null=True)
float1_name = models.CharField(max_length=60, blank=True, null=True)
float1_default = models.FloatField(blank=True, null=True)
float2_name = models.CharField(max_length=60, blank=True, null=True)
float2_default = models.FloatField(blank=True, null=True)
string1_name = models.CharField(max_length=60, blank=True, null=True)
string1_default = models.CharField(max_length=60, blank=True, null=True)
string2_name = models.CharField(max_length=60, blank=True, null=True)
string2_default = models.CharField(max_length=60, blank=True, null=True)
Run Code Online (Sandbox Code Playgroud)
然后存储它们
class ItemData(models.Model):
created = models.DateTimeField(default=datetime.now)
item = models.ForeignKey(Item, editable=False)
int1_val = models.IntegerField(blank=True, null=True)
int2_val = models.IntegerField(blank=True, …Run Code Online (Sandbox Code Playgroud) 我有一个很大的清单
[[1,.., ..],[2,...,...],[5,...,...],[1,...,...]]
Run Code Online (Sandbox Code Playgroud)
我需要删除所有具有相同第一个值的元素。(只保留一次)
怎么做最有效率?
我正在尝试使用iexact我的Django应用程序.
我在我的数据库类的物品test,TEST,tEsT,和TesT.
我试图找出test我的数据库是否有任何形式.好像我需要使用iexact但是我尝试使用它我收到了一个错误.
这是我的代码片段.
def item_search(x):
item = x.column_in_database
if test__iexact = 'test' in item:
return 1; #this is just pseduocode for stackoverflow
elif
return 0; #this is just pseduocode for stackoverflow
Run Code Online (Sandbox Code Playgroud)
我尝试过各种各样的方法,但我仍然无法让它发挥作用.
我用python 2.7.2运行Ubuntu.
脚本
python \
/home/blainer/Desktop/convert/converter.py \
/home/blainer/Desktop/convert/urban.shp \
/home/blainer/Desktop/convert/result.js \
--width 900 \
--country_name_index 4 \
--where "ISO = 'USA'" \
--codes_file /home/blainer/Desktop/convert/codes-en.tsv \
--insets '[{"codes": ["US-AK"], "width": 200, "left": 10, "top": 370}, {"codes": ["US-HI"], "width": 100, "left": 220, "top": 400}]' \
--minimal_area 4000000 \
--buffer_distance -3000 \
--simplify_tolerance 1000 \
--longtitude0 10w \
--name us
Run Code Online (Sandbox Code Playgroud)
错误
blainer@ubuntu:~/Desktop/convert$ python script.py
File "script.py", line 5
--width 900 \
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)