我有这个函数从文件中读取文本:
uintmax_t ResourcePack::getText(const string& file, char** data)
{
*data = new char[static_cast<size_t>(size) + 1];
fseek(_fileDescriptor, static_cast<long>(begin), SEEK_SET);
fread(*data, static_cast<size_t>(size), 1, _fileDescriptor);
*data[size] = '\0';
}
Run Code Online (Sandbox Code Playgroud)
FILE* _fileDescriptor, uintmax_t size并uintmax_t begin获得其他代码,这里不重要,但具有正确的值.
fseek和fread线条工作正常.实际上,我有*数据中的文件内容,但是当执行最后一行时,我得到了访问冲突.
为什么我可以写入*data使用fread,但不是using *data[size] = '\0'?
所以我有一个字符串
s = '>n269412 | AK142815 | msdfhakjfdkjfs'
Run Code Online (Sandbox Code Playgroud)
我希望包括所有内容,但不包括第一个'|'实例
所以我做的是
import re
p = re.search('|',s)
print s[:p]
Run Code Online (Sandbox Code Playgroud)
但我得到了这个错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: slice indices must be integers or None or have an __index__ method
Run Code Online (Sandbox Code Playgroud)
我明白为什么它不起作用..因为那个值不是一个整数但有什么方法可以在搜索找到该元素时使用该值?
我刚刚开始使用Python进行编程,而且我没有定义x,但我不知道为什么它会说考虑到我认为它是定义的.
def menu():
x = 0
while x != 1 or 2:
print "menu"
print "1)login"
print "2)under dev"
x = raw_input('select menu option')
if x == 1 or 2:
break
menu()
if x=='1':
print "enter username:"
y = raw_input()
if y=="username":
print "enter password:"
z = raw_input()
if z=="password":
print "password accepted"
elif x=='2':
print "under development"
elif y or z == False:
print "username or password incorrect"
Run Code Online (Sandbox Code Playgroud) 我想将python中的数字转换为3个字符的字符串,前缀为0.例如:
8 = "008"
12 = "012"
6 = "006"
Run Code Online (Sandbox Code Playgroud)
谢谢
我目前有这个代码:
yon = raw_input("were you running in km y or n?: ")
if yon is "y":
kilnumber = raw_input("how many kilometers?: ")
milnumber = 0
try:
float(kilnumber)
except:
print "You must enter a number"
exit()
Run Code Online (Sandbox Code Playgroud)
如果用户输入非法响应,我希望通过添加while循环来重新询问kilnumber来简化它.我希望这样的事情:
yon = raw_input("were you running in km y or n?: ")
if yon is 'y':
kilnumber = raw_input("how many kilometers?: ")
milnumber = 0
while float(kilnumber) is ValueError:
print "You must enter a number"
kilnumber = raw_input("how many kilometers?: ")
Run Code Online (Sandbox Code Playgroud)
此代码不起作用,因为它在评估while循环之前发送错误消息.解?
我有以下代码:
dict = {
'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2, 'h': 4, 'i': 1,
'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1, 'o': 1, 'p': 3, 'q': 10,
'r': 1, 's': 1, 't': 1, 'u': 1, 'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10
}
word = 'banana'
for letter in word:
print dict[letter]
Run Code Online (Sandbox Code Playgroud)
我得到以下输出
3
1
1
1
1
Run Code Online (Sandbox Code Playgroud)
如何添加这些值?
那就是我如何将输出打印为8
我试图通过看起来像这样的字典创建一个查询:
名称:name(第二个名称是用户通过raw_input键入的预设值)
ID:id(此处相同)
出生日期:dob(和此处相同)
这是我当前的代码:
students[id] = {
"\nName":name,
"\nDate of Birth":dob,
"ID":id,
}
Run Code Online (Sandbox Code Playgroud)
结果如下:
{'\nName':'示例名称','\n出生日':'样例出生日','ID':'样本ID'}
我知道你需要在字符串前打印/打印才能使新行正常工作,但是我无法在字典中使用它.有没有办法绕过这个?
我想在python版本2.7中使用%sin raw_input.我写了这段代码:
name = raw_input("Hello, please insert your name: ")
acpass = raw_input("Hi %s please insert your account password") % name
Run Code Online (Sandbox Code Playgroud)
它不起作用.出现此错误:
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
为什么?
这是我的代码,使用find_all,但它适用于.find():
import requests
from BeautifulSoup import BeautifulSoup
r = requests.get(URL_DEFINED)
print r.status_code
soup = BeautifulSoup(r.text)
print soup.find_all('ul')
Run Code Online (Sandbox Code Playgroud)
这就是我得到的:
Traceback (most recent call last):
File "scraper.py", line 19, in <module>
print soup.find_all('ul')
TypeError: 'NoneType' object is not callable
Run Code Online (Sandbox Code Playgroud) 以下如何工作?
>>> 3*[2]
>>> [2,2,2]
>>> [2]*3
>>> [2,2,2]
Run Code Online (Sandbox Code Playgroud)
我明白这*是位置扩展运算符.既然[2]是一个包含单个项目的列表,我看不出如何3*[2]扩展到任何有意义的东西,我期待一个SyntaxError,但事实并非如此.
我很难找到现有的答案,我找到的只是引用*args和**kwargs传递可变参数列表,这些都没有完全回答我的问题.
python ×9
python-2.7 ×4
python-3.x ×2
c++ ×1
dictionary ×1
list ×1
loops ×1
search ×1
string ×1