我喜欢在Python脚本中转换以下字符串:
mystring='(5,650),(235,650),(465,650),(695,650)'
Run Code Online (Sandbox Code Playgroud)
到元组列表
mytuple=[(5,650),(235,650),(465,650),(695,650)]
Run Code Online (Sandbox Code Playgroud)
这样
print mytuple[0]得到:
(5,650)
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用getaddrinfo()解析URL的IP地址,但它总是返回错误的IP地址,我尝试了几个URL,结果是一样的.非常感谢任何帮助.
该程序返回IP地址:209.85.175.99返回真实IP,即74.125.39.147
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
int
main()
{
char *hostname = "www.google.lk";
struct addrinfo hints, *res;
struct in_addr addr;
int err;
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;
if ((err = getaddrinfo(hostname, NULL, &hints, &res)) != 0) {
printf("error %d\n", err);
return 1;
}
addr.s_addr = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
printf("ip address : %s\n", inet_ntoa(addr));
freeaddrinfo(res);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 如果列表列表中的每个元素都大于其neigbbour,那么计算最"pythonic"的方法是什么?例如
a = [[3.1, 3.13], [3.14, 3.12], [3.12, 3.1]]
Run Code Online (Sandbox Code Playgroud)
我想看看每个列表中的第一个元素(在较大列表中)是否大于第二个元素.所以对于第一项,它的假,因为3.1 <3.13.第2和第3项是真的.
我当然可以使用for循环,但是希望看到替代方法.谢谢.
任何人都知道ino_t类型的占位符是什么?我正在尝试使用printf打印出来并尝试了%d,%i,%s和其他但不能正常工作.
printf( " file name = %s, i-node number=**%d**\n", direntp->d_name, direntp->d_ino);
warning: format ‘%i’ expects argument of type ‘int’, but argument 3 has type ‘__ino_t’ [-Wformat]
Run Code Online (Sandbox Code Playgroud)
假设我的其他代码是正确的.大多数示例仅显示如何打印名称,但不显示inode编号.我也搜过很多地方.
提前致谢
我有一个应用程序,其中各种实体通过套接字相互通信,我正在使用C编程语言.当实体向另一个实体发送长消息时,recv()函数可能会部分地读取此消息.因此,我必须通过附加所有收到的部分在接收方重新构建消息.
我的问题是与recv()相关的一般套接字编程问题.recv()如何知道消息何时被完全读取?我应该使用"\n"这样的特殊字符终止消息吗?或者我应该将邮件的大小作为标题发送?常见的做法是什么?
我是 Python 新手。我有一个带有 3 个属性(名称、价格、图像)的 Item 表,我想从链接中获取 html 并保存到表中。所以我这样做
from books.models import Book, Author, Publisher, Item
import urllib
import gzip
from StringIO import StringIO
from BeautifulSoup import BeautifulSoup
import MySQLdb as mdb
def updateItems(request):
con = mdb.connect('localhost', 'anhnt', '12', 'db_django');
cur = con.cursor()
link = "http://pandagift.vn/i263/price1/qua-tang-doc-dao.htm"
request = urllib.urlopen(link)
output1 = open('web.txt', 'wb')
html = ""
html = request.read()
output1.write(html)
output1.close()
beautifulSoup = BeautifulSoup(html)
list_item = beautifulSoup.findAll("div", {"class":"Item_ProcNews"})
if list_item:
for item in list_item :
#lay anh san pham
image …Run Code Online (Sandbox Code Playgroud) 试图学习一些地理空间蟒蛇.或多或少遵循这里的课堂笔记.
#!/usr/bin/python
# import modules
import ogr, sys, os
# set working dir
os.chdir('/home/jacques/misc/pythongis/data')
# create the text file we're writing to
file = open('data_export.txt', 'w')
# import the required driver for .shp
driver = ogr.GetDriverByName('ESRI Shapefile')
# open the datasource
data = driver.Open('road_surveys.shp', 1)
if data is None:
print 'Error, could not locate file'
sys.exit(1)
# grab the datalayer
layer = data.GetLayer()
# loop through the features
feature = layer.GetNextFeature()
while feature:
# acquire attributes
id …Run Code Online (Sandbox Code Playgroud) if groupName.group == "None":
Run Code Online (Sandbox Code Playgroud)
错误:
AttributeError: 'NoneType' object has no attribute 'group'
Run Code Online (Sandbox Code Playgroud)
如何检查对象是否具有属性?
想象一下以下场景:
class A:
def __init__(self, arg1=3, arg2=5):
pass
def createA(arg1=None, arg2=None):
if arg1 is None:
if arg2 is None:
a = A()
else:
a = A(arg2=arg2)
else:
if arg2 is None:
a = A(arg1=arg1)
else:
a = A(arg1=arg1, arg2=arg2)
return a
Run Code Online (Sandbox Code Playgroud)
什么是实现此行为的最佳方式,请注意以下事项:
AA构造函数的参数的默认值添加到createA函数中?例如,是否有任何值表示未通过可选参数?就像是:
if arg1 is None:
newArg1 = NotPassed
else:
newArg1 = arg1
if arg2 is None:
newArg2 = NotPassed
else:
newArg2 = arg2
A(arg1=newArg1, arg2=newArg2)
Run Code Online (Sandbox Code Playgroud) 在下面的代码中,我试图使用python脚本创建一个"更多"命令(unix),方法是将文件读入列表并一次打印10行,然后询问用户是否要打印下10行(打印更多. ).问题是raw_input一次又一次地要求输入,如果我给'y'或'Y'作为输入并且不继续while循环并且如果我给任何其他输入while循环制动.我的代码可能不是最好的学习python.
import sys
import string
lines = open('/Users/abc/testfile.txt').readlines()
chunk = 10
start = 0
while 1:
block = lines[start:chunk]
for i in block:
print i
if raw_input('Print More..') not in ['y', 'Y']:
break
start = start + chunk
Run Code Online (Sandbox Code Playgroud)
我得到的输出代码是: -
--
10 lines from file
Print More..y
Print More..y
Print More..y
Print More..a
Run Code Online (Sandbox Code Playgroud) python ×7
c ×3
linux ×2
sockets ×2
configparser ×1
geospatial ×1
getaddrinfo ×1
ip ×1
ip-address ×1
mysql ×1
mysql-python ×1
raw-input ×1
while-loop ×1