当文件名中存在unicode时,文件未在python中创建
我需要将HTML文件存储为文本文件。以网络标题的相同名称。我的代码出了问题,所以它没有在目录中创建文件。我有写的目录权限。我正在使用Ubuntu12.04LTS 目录当文件名中存在unicode时,文件未在python中创建,python,Python,我需要将HTML文件存储为文本文件。以网络标题的相同名称。我的代码出了问题,所以它没有在目录中创建文件。我有写的目录权限。我正在使用Ubuntu12.04LTS 目录/home/user1/ 文件名 打印名称=Mathrubhumi Sports-ശ്രീനിക്ക് പച്ചക്കൊടി 文件名包含Unicode值 import os from urllib import urlopen from bs4 import BeautifulSoup url= "htt
/home/user1/
文件名
打印名称=Mathrubhumi Sports-ശ്രീനിക്ക് പച്ചക്കൊടി
文件名包含Unicode值
import os
from urllib import urlopen
from bs4 import BeautifulSoup
url= "http://www.mathrubhumi.com/sports/story.php?id=397111"
raw = urlopen(url).read()
soup = BeautifulSoup(raw,'lxml')
texts = soup.findAll(text=True)
name = soup.title.text
name= name+'.txt'
def contains_unicode(text):
try:
str(text)
except:
return True
return False
result = ''.join((text for text in texts if contains_unicode(text)))
# Output to a file
with open(os.path.join('/home/user1/textfiles',name,'w') as out:
out.write(result)
请帮我调试它我试过了,它成功了,它在当前目录中创建了一个名为Mathrub*.txt的文件,其中包含一些文本
import codecs
import os
from urllib import urlopen
from bs4 import BeautifulSoup
url= "http://www.mathrubhumi.com/sports/story.php?id=397111"
raw = urlopen(url).read()
soup = BeautifulSoup(raw,'lxml')
texts = soup.findAll(text=True)
name = soup.title.string
name= name+'.txt'
def contains_unicode(text):
try:
str(text)
except:
return True
return False
result = ''.join((text for text in texts if contains_unicode(text)))
# Output to a file
with codecs.open(name,'w',encoding="utf-8") as out:
out.write(result)
在添加编解码器部分之前,它大声抱怨试图写入一些它不知道如何解释的字符。本地化许可证?您是否收到任何错误?哪个?是的,我有权限写哪个目录?
name
的值是多少?例如,无法使用/创建文件