我在路径中添加了〜/ Scripts文件夹中的一些脚本.所以我试着通过调用它们来测试我是否可以运行它们.我在Linux Mint 11上有python 3.1.
user@pc ~/Scripts $ python aek.py
AEK
user@pc ~/Scripts $ aek.py
/home/user/Scripts/aek.py: line 1: syntax error near unexpected token `'AEK''
/home/user/Scripts/aek.py: line 1: `print('AEK')'
Run Code Online (Sandbox Code Playgroud)
代码就是这一行:
print('AEK')
Run Code Online (Sandbox Code Playgroud) 你好我每个人都写过代码
char sentence[100];
scanf("%s" ,sentence);
char * ptrs = sentence ;
printf("%d", strlen(ptrs));
Run Code Online (Sandbox Code Playgroud)
假设我进入
john is a boy
Run Code Online (Sandbox Code Playgroud)
该strlen()功能是给我值4停止的空间是我应该做的感谢后,计数
我正在尝试编写一个Python程序,但我很难获得分数.我把它写成一个值返回函数,每次运行程序时,它似乎跳过它检索得分的步骤,除非我包含一个else语句,它会自动跳过else语句.我将附上以下完整代码.非常感谢你的帮助,我很高兴!这也是我第一次在这个论坛发帖,如果我搞砸了,我道歉.
#constants
Rock = 1
Paper = 2
Scissors = 3
#Define the main function
def main():
#set control loop
keep_going = 'Y'
#set counter to zero
computer_wins = 0
player_wins = 0
tie_score = 0
#call display message
display_message()
while keep_going == 'y' or keep_going == 'Y':
play_game()
#prompt user to keep going
keep_going = input('would you like to play again? (Y for Yes): ')
print('The computer won', computer_wins, 'times')
print('The player won', player_wins, 'times')
print('There were', tie_score, 'tie …Run Code Online (Sandbox Code Playgroud) 我正在使用Python 3测试一些非常简单的代码.出于某种原因,下面的代码生成一个空输出,而不是写一行
import csv
output = open('hello world.csv', 'w',newline='')
wr = csv.writer(output, quoting=csv.QUOTE_ALL)
wr.writerow('hello world')
Run Code Online (Sandbox Code Playgroud)
如果我在解释器中尝试命令,则 .writerow()返回一个数字.以下示例是文档页面中使用的示例:
>> import csv
>>> spamWriter = csv.writer(open('eggs.csv', 'w', newline=''), delimiter=' ',
... quotechar='|', quoting=csv.QUOTE_MINIMAL)
>>> spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
40
>>> spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
37
Run Code Online (Sandbox Code Playgroud) 我想在标签下使用HTMLParser获取整个html.我目前能够获取标签之间的数据,以下是我的代码
class LinksParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.recording = 0
self.data = ''
def handle_starttag(self, tag, attributes):
if tag != 'span':
return
if self.recording:
self.recording += 1
return
for name, value in attributes:
if name == 'itemprop' and value == 'description':
break
else:
return
self.recording = 1
def handle_endtag(self, tag):
if tag == 'span' and self.recording:
self.recording -= 1
def handle_data(self, data):
if self.recording:
self.data += data
Run Code Online (Sandbox Code Playgroud)
我也想在输入中使用html标签
<span itemprop="description">
<h1>My First Heading</h1>
<p>My first <br/><br/>paragraph.</p>
</span>
Run Code Online (Sandbox Code Playgroud)
当提供输入时,只会给我带有标签的数据.有没有什么方法可以在标签之间获得整个html?
了解,使用str.join运营商是"选择"的方式来连接在Python中,我徘徊在那里的类型啄食顺序操作:
"%s %s" % (first_name, last_name)
Run Code Online (Sandbox Code Playgroud)
适合.它们比使用更快还是更慢+?
我的代码就像
a = "*abc*bbc"
a.split("*")#['','abc','bbc']
#i need ["*","abc","*","bbc"]
a = "abc*bbc"
a.split("*")#['abc','bbc']
#i need ["abc","*","bbc"]
Run Code Online (Sandbox Code Playgroud)
如何在python split函数或regex或分区中获取带分隔符的列表?我正在使用python 2.7,windows
我正在研究当前的玩具代码,试图理解asyncio模块.
import asyncio
import os, sys, traceback
from time import time
os.environ['PYTHONASYNCIODEBUG'] = '1'
print(sys.version)
def timed_fib(n):
def fib(n):
return fib(n - 1) + fib(n - 2) if n > 1 else n
a = time()
return fib(n), time() - a
def process_input():
text = sys.stdin.readline()
n = int(text.strip())
print('fib({}) = {}'.format(n, timed_fib(n)))
@asyncio.coroutine
def print_hello():
while True:
print("{} - Hello world!".format(int(time())))
yield from asyncio.sleep(3)
def main():
loop = asyncio.get_event_loop()
loop.add_reader(sys.stdin, process_input)
loop.run_until_complete(print_hello())
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
然而,尝试运行它会产生下面令人难以置信的神秘回溯.如您所见,调试环境变量在上面代码的第五行中设置,但是,回溯仍然非常无益,如下所示:
3.4.3rc1 …Run Code Online (Sandbox Code Playgroud) 我仍然不完全理解python的unicode和str类型是如何工作的.注意:我在Python 2中工作,据我所知,Python 3对同一问题有完全不同的方法.
我所知道的:
str 是一种较老的野兽,可以保存由历史迫使我们使用的太多编码之一编码的字符串.
unicode 是一种更标准化的方式来表示字符串使用一个巨大的表格,包括所有可能的字符,表情符号,狗屎的小图片等等.
该decode函数将字符串转换为unicode,反之亦然encode.
如果我,在python的shell中,只需说:
>>> my_string = "some string"
Run Code Online (Sandbox Code Playgroud)
然后my_string是一个str编码的变量ascii(并且,因为ascii是utf-8的子集,它也被编码utf-8).
因此,例如,我可以unicode通过说出其中一行来将其转换为变量:
>>> my_string.decode('ascii')
u'some string'
>>> my_string.decode('utf-8')
u'some string'
Run Code Online (Sandbox Code Playgroud)
我不知道的是:
Python如何处理在shell中传递的非ascii字符串,并且知道这一点,保存单词的正确方法是"kožuš?ek"什么?
例如,我可以说
>>> s1 = 'kožuš?ek'
Run Code Online (Sandbox Code Playgroud)
在这种情况下s1成为str我无法转换为的实例unicode:
>>> s1='kožuš?ek'
>>> s1
'ko\x9eu\x9a\xe8ek'
>>> print s1
kožuš?ek
>>> s1.decode('ascii')
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
s1.decode('ascii') …Run Code Online (Sandbox Code Playgroud) 从源代码我以JSON格式检索一些数据.我想将这些数据(及时测量)保存为文本文件.我反复想要使用相同的信号源并查看是否有新的测量值,如果是,我想将其添加到其他测量值.
我得到的数据如下:
{"xyz":[{"unixtime":"1458255600","time":"00:00","day":"18\/03","value":"11","paramlabel":"30-500 mHz","popupcorr":"550","iconnr":"7","paramname":"30-500 mHz"},{"unixtime":"1458256200","time":"00:10","day":"18\/03","value":"14","paramlabel":"30-500 mHz","popupcorr":"550","iconnr":"7","paramname":"30-500 mHz"},etc.]}
Run Code Online (Sandbox Code Playgroud)
我将这些数据加载到pandas DataFrame中,以便能够更轻松地使用它.但是,当我将其加载到数据框中时,所有列都被视为字符串.如何确保将unixtime列视为时间戳(以便我可以转换为日期时间)?
python ×8
python-3.x ×3
string ×2
arrays ×1
c ×1
csv ×1
datetime ×1
encoding ×1
html-parsing ×1
pandas ×1
performance ×1
python-2.7 ×1
python-2.x ×1
regex ×1
strlen ×1
timestamp ×1
unicode ×1
utf-8 ×1
windows ×1