the differences between python2.x and python3.x as you tried to write words into a file
#Under the python 3.x
def combineWordsFromFeed(filename):
#with open(filename, 'w',encoding='utf-8') as wfile: #for python 3.x
with open(filename, 'w') as wfile: #for python 2.x
for feed in feedlist:
print("Parsing " + feed)
fp = feedparser.parse(feed)
for e in fp.entries:
#txt = e.title+ extractPlainText(e.description)#for python 3.x
txt = e.title.encode('utf8') +extractPlainText(e.description.encode('utf8'))#for python 2.x
words = separatewords(txt)
#print(words)
for word in words:
if word.isdigit() == False and word not in mystopwords:
wfile.write(word)
wfile.write(" ")
wfile.write("\n")
wfile.close()
return
output:
TypeError Traceback (most recent call last)
<ipython-input-65-08ed4df9ed8a> in <module>()
----> 1 combineWordsFromFeed("wordcloudInput_FromFeeds.txt")
<ipython-input-64-a19447dbe8ed> in combineWordsFromFeed(filename)
8 for e in fp.entries:
9 #txt = e.title+ extractPlainText(e.description)#for python 3.x
---> 10 txt = e.title.encode('utf8') +extractPlainText(e.description.encode('utf8'))#for python 2.x
11 words = separatewords(txt)
12
TypeError: can't concat str to bytes
###############################################################
def combineWordsFromFeed(filename):
#with open(filename, 'w',encoding='utf-8') as wfile: #for python 3.x
with open(filename, 'w') as wfile: #for python 2.x
for feed in feedlist:
print("Parsing " + feed)
fp = feedparser.parse(feed)
for e in fp.entries:
#txt = e.title+ extractPlainText(e.description)#for python 3.x
txt = e.title.encode('utf8') #for python 2.x
words = separatewords(txt)
#print(words)
for word in words:
if word.isdigit() == False and word not in mystopwords:
wfile.write(word)
wfile.write(" ")
wfile.write("\n")
wfile.close()
return
output:
TypeError Traceback (most recent call last)
<ipython-input-67-08ed4df9ed8a> in <module>()
----> 1 combineWordsFromFeed("wordcloudInput_FromFeeds.txt")
<ipython-input-66-bfa60f6626f9> in combineWordsFromFeed(filename)
9 #txt = e.title+ extractPlainText(e.description)#for python 3.x
10 txt = e.title.encode('utf8')
---> 11 words = separatewords(txt)
12
13 #print(words)
<ipython-input-63-06f3809616f7> in separatewords(text)
1 def separatewords(text=' '):
2 splitter = re.compile('\\W*')
----> 3 return [s.lower() for s in splitter.split(text) if len(s) >3]
TypeError: cannot use a string pattern on a bytes-like object
###############################################################
solution:
def combineWordsFromFeed(filename):
with open(filename, 'w',encoding='utf-8') as wfile: #for python 3.x
#with open(filename, 'w') as wfile: #for python 2.x
for feed in feedlist:
print("Parsing " + feed)
fp = feedparser.parse(feed)
for e in fp.entries:
txt = e.title+ extractPlainText(e.description)#for python 3.x
#txt = e.title.encode('utf8') +extractPlainText(e.description.encode('utf8'))#for python 2.x
words = separatewords(txt)
#print(words)
for word in words:
if word.isdigit() == False and word not in mystopwords:
wfile.write(word)
wfile.write(" ")
wfile.write("\n")
wfile.close()
return
###############################################################
#Under the python 2.x
def combineWordsFromFeed(filename):
with open(filename, 'w',encoding='utf-8') as wfile: #for python 3.x
#with open(filename, 'w') as wfile: #for python 2.x
for feed in feedlist:
print("Parsing " + feed)
fp = feedparser.parse(feed)
for e in fp.entries:
txt = e.title+ extractPlainText(e.description)#for python 3.x
#txt = e.title.encode('utf8') +extractPlainText(e.description.encode('utf8'))#for python 2.x
words = separatewords(txt)
#print(words)
for word in words:
if word.isdigit() == False and word not in mystopwords:
wfile.write(word)
wfile.write(" ")
wfile.write("\n")
wfile.close()
return
TypeError Traceback (most recent call last)
<ipython-input-7-08ed4df9ed8a> in <module>()
----> 1 combineWordsFromFeed("wordcloudInput_FromFeeds.txt")
<ipython-input-6-26ff6499205a> in combineWordsFromFeed(filename)
1 def combineWordsFromFeed(filename):
----> 2 with open(filename, 'w',encoding='utf-8') as wfile: #for python 3.x
3 #with open(filename, 'w') as wfile: #for python 2.x
4 for feed in feedlist:
5 print("Parsing " + feed)
TypeError: 'encoding' is an invalid keyword argument for this function
Solution
def combineWordsFromFeed(filename):
#with open(filename, 'w',encoding='utf-8') as wfile: #for python 3.x
with open(filename, 'w') as wfile: #for python 2.x
for feed in feedlist:
print("Parsing " + feed)
fp = feedparser.parse(feed)
for e in fp.entries:
#txt = e.title+ extractPlainText(e.description)#for python 3.x
txt = e.title.encode('utf8') +extractPlainText(e.description.encode('utf8'))#for python 2.x
words = separatewords(txt)
#print(words)
for word in words:
if word.isdigit() == False and word not in mystopwords:
wfile.write(word)
wfile.write(" ")
wfile.write("\n")
wfile.close()
return
本文探讨了Python2.x与3.x在处理文件写入操作时的主要区别,尤其是在尝试将字符串写入文件时遇到的不同错误类型。通过对比示例代码,解释了如何在不同版本中正确处理字符串编码和字节串,以避免TypeError。
3057

被折叠的 条评论
为什么被折叠?



