我有点像一个python /编程新手,我刚刚玩弄字符串切片.所以简单的字符串反向方法就string[::-1]可以正常工作,但是我的代码中还有其他实例可以产生意想不到的结果:
In [1]: string = "Howdy doody"
In [2]: string[::]
Out[2]: 'Howdy doody'
In [3]: string[::-1]
Out[3]: 'ydood ydwoH'
In [4]: string[0:]
Out[4]: 'Howdy doody'
In [5]: string[0::-1]
Out[5]: 'H' # what up with this?
In [6]: string[:len(string)]
Out[6]: 'Howdy doody'
In [7]: string[:len(string):-1]
Out[7]: '' # what up with this too?
In [8]: string[0:len(string)]
Out[8]: 'Howdy doody'
In [9]: string[0:len(string):-1]
Out[9]: '' # And what up here too.
Run Code Online (Sandbox Code Playgroud)
我已经评论了上面的行,我希望字符串可以反转,但我很惊讶他们为什么不简单地反转字符串.谁知道那是什么?
from amazon.api import AmazonAPI
AMAZON_ACCESS_KEY = "A******************A"
AMAZON_SECRET_KEY = "7***********************E"
AMAZON_ASSOC_TAG = "j*****-20"
amazon = AmazonAPI(AMAZON_ACCESS_KEY, AMAZON_SECRET_KEY, AMAZON_ASSOC_TAG, region='US')
print(amazon)
#product = amazon.lookup(ItemId='B002RL8FBQ')
Run Code Online (Sandbox Code Playgroud)
当我运行上面的代码时,它工作正常,我从打印功能获得此输出:
<amazon.api.AmazonAPI object at 0x7fb6e59f7b38>
因此,我的访问密钥,密钥和关联标记一切正常.
但是,如果我取消注释最后一行,#product = amazon.lookup(ItemId='B00EOE0WKQ')那么我会收到此错误回溯:
Traceback (most recent call last):
File "test.py", line 8, in <module>
product = amazon.lookup(ItemId='B00EOE0WKQ')
File "/home/darren/Python_projects/amazon_wp/myvenv/lib/python3.4/site-packages/amazon/api.py", line 173, in lookup
response = self.api.ItemLookup(ResponseGroup=ResponseGroup, **kwargs)
File "/home/darren/Python_projects/amazon_wp/myvenv/lib/python3.4/site-packages/bottlenose/api.py", line 251, in __call__
{'api_url': api_url, 'cache_url': cache_url})
File "/home/darren/Python_projects/amazon_wp/myvenv/lib/python3.4/site-packages/bottlenose/api.py", line 212, in _call_api
return urllib2.urlopen(api_request, timeout=self.Timeout)
File "/usr/lib/python3.4/urllib/request.py", line …Run Code Online (Sandbox Code Playgroud) 我有一个脚本应该附加到文件,但它引发了一个我不理解的错误,不知道它是如何被触发的.
这是代码:
import re
num_words = "four kiddiewinks|four children|four kids"
words_list = num_words.split('|')
def append_2synonym(words_list, num_words):
with open('test2 words.txt', 'a+') as f:
read_f = f.read()
patt = r'^' + words_list[0] + '\|'
result = re.search(patt, read_f, re.MULTILINE)
if result == None:
f.write("\n" + num_words)
else:
print "\nNo match found in '2 words.txt' file"
append_2synonym(words_list, num_words)
Run Code Online (Sandbox Code Playgroud)
这是'test2 words.txt'文件的内容:
five kiddiewinks|five kids|five children
mobile phone|cell phone|cellular phone
stinky cheese|smelly cheese
Run Code Online (Sandbox Code Playgroud)
这是我得到的完整错误:
Traceback (most recent call last):
File "D:\Magic Briefcase\My Python Scripts\Spin Scripts\synonyms\testing2.py", …Run Code Online (Sandbox Code Playgroud) 我是编程的新手,过去几个月我一直在闲暇时间学习python.我决定尝试创建一个小脚本,在文本文件中将美国拼写转换为英语拼写.
在过去的5个小时里,我一直在尝试各种各样的事情,但最终想出的东西让我更接近我的目标,但并不完全在那里!
#imported dictionary contains 1800 english:american spelling key:value pairs.
from english_american_dictionary import dict
def replace_all(text, dict):
for english, american in dict.iteritems():
text = text.replace(american, english)
return text
my_text = open('test_file.txt', 'r')
for line in my_text:
new_line = replace_all(line, dict)
output = open('output_test_file.txt', 'a')
print >> output, new_line
output.close()
Run Code Online (Sandbox Code Playgroud)
我确信有一个更好的方法可以解决问题,但对于这个脚本,这里是我遇到的问题:
任何帮助赞赏这个渴望新手!
test_file.txt的内容是:
I am sample file.
I contain an english spelling: colour.
3 american spellings on 1 line: color, analyze, utilize.
1 american spelling on 1 line: …Run Code Online (Sandbox Code Playgroud) 通过昨天的问题得到帮助 - Python 2.7 - 从文本文件中查找和替换,使用字典,到新的文本文件 - 我今天开始学习正则表达式,以了解@Blckknght为其创建的正则表达式代码我的回答.
但是,在我看来,python文档(或者更可能是我)在\b代码方面略有不正确.我在关于\ b的python文档中引用的部分是这样的:
例如,r'\ bfoo\b'匹配'foo','foo.','(foo)','bar foo baz'但不匹配'foobar'或'foo3'.
(链接到该页面 http://docs.python.org/2/library/re.html)
我无法理解 'bar foo baz'比赛?例如,如果我创建此代码:
import re
m = re.search(r'\bfoo\b', 'bar foo baz')
m.group()
Run Code Online (Sandbox Code Playgroud)
...然后我从控制台得到这个结果:
'foo'
Run Code Online (Sandbox Code Playgroud)
... 并不是
'bar foo baz'
Run Code Online (Sandbox Code Playgroud)
事实上,基于python文档中关于'\ b'的其余解释,我实际上会期待 'foo'打印到控制台,因为它匹配单词开头和结尾的空字符串.
那么,python文档中的交易是什么呢 'bar foo baz'的匹配是什么?
编辑:我正在使用python 2.7
这里有点蟒蛇/编程新手......
我试图想出一个正则表达式,它可以处理从文本文件中的一行中提取句子,然后将它们附加到列表中.代码:
import re
txt_list = []
with open('sample.txt', 'r') as txt:
patt = r'.*}[.!?]\s?\n?|.*}.+[.!?]\s?\n?'
read_txt = txt.readlines()
for line in read_txt:
if line == "\n":
txt_list.append("\n")
else:
found = re.findall(patt, line)
for f in found:
txt_list.append(f)
for line in txt_list:
if line == "\n":
print "newline"
else:
print line
Run Code Online (Sandbox Code Playgroud)
根据上述代码的最后5行打印输出:
{Hello there|Hello|Howdy} Dr. Munchauson you {gentleman|fine fellow}!
What {will|shall|should} we {eat|have} for lunch? Peas by the {thousand|hundred|1000} said Dr. Munchauson; {that|is} what he said.
newline
I am the {very …Run Code Online (Sandbox Code Playgroud) 我是一个Python newb试图更好地理解正则表达式.就在我认为我已经掌握了基础知识的时候,有些东西会让我失望 - 比如下面的内容:
>>> import re
>>> text = "Some nouns like eggs egg bacon what a lovely donkey"
>>> noun_list = ['eggs', 'bacon', 'donkey', 'dog']
>>> noun_patt = r'\s' + '|'.join(noun_list) + r'\s'
>>> found = re.findall(noun_patt, text)
>>> found
[' eggs', 'bacon', 'donkey']
Run Code Online (Sandbox Code Playgroud)
因为我设置了正则表达式模式来找到'whitespace' + 'pipe joined list of nouns' + 'whitespace'- 为什么:
' eggs'被发现之前有空间而不是之后?
'bacon'被发现它的两边都没有空格?
'donkey'被发现它的两边都没有空格,之后没有空格?
结果我期待: [' eggs ', ' bacon ']
我使用的是Python 2.7
Python /编程新手,尝试弄清楚这个while循环的内容.首先是代码:
var_list = []
split_string = "pink penguins,green shirts,blue jeans,fried tasty chicken,old-style boots"
def create_variations(split_string):
init_list = split_string.split(',')
first_element = init_list[0]
# change first element of list to prepare for while loop iterations
popped = init_list.pop()
added = init_list.insert(0, popped)
while init_list[0] != first_element:
popped = init_list.pop()
added = init_list.insert(0, popped)
print init_list # prints as expected, with popped element inserted to index[0] on each iteration
var_list.append(init_list) # keeps appending the same 'init_list' as defined on line 5, not …Run Code Online (Sandbox Code Playgroud) 我是一个Python /编程新手,我试图第一次使用python类.
在这段代码中,我试图创建一个脚本来备份一些文件.我总共有6个文件,我想用这个脚本定期备份,所以我想我会尝试使用python类来保存我写出来的东西6次,并且还可以使用Classes进行练习.
在我的下面的代码中,我已经设置了一些东西,仅用于创建一个类的实例来进行测试.但是,我遇到了麻烦.我似乎无法使用运算符分配原始文件名和备份文件名.
打开文件时是否无法使用运算符作为文件名?或者我做错了什么.
class Back_up(object):
def __init__(self, file_name, back_up_file):
self.file_name = file_name
self.back_up_file = back_up_file
print "I %s and me %s" % (self.file_name, self.back_up_file)
with open('%s.txt', 'r') as f, open('{}.txt', 'w') as f2 % (self.file_name, self.back_up_file):
f_read = read(f)
f2.write(f_read)
first_back_up = Back_up("syn1_ready", "syn1_backup")
Run Code Online (Sandbox Code Playgroud)
另外,7号线真的很长,任何关于如何缩短它的提示都很受欢迎.
谢谢达伦
python ×9
regex ×4
class ×1
list ×1
operators ×1
python-2.7 ×1
python-3.4 ×1
reverse ×1
slice ×1
string ×1
while-loop ×1