錯誤描述:
IOError: [Errno 22] invalid mode ('wb') or filename: 'C:\\Users\\Viral Patel\\Documents\\GitHub\\3DPhotovoltaics\\Data_Output\\Simulation_Data\\Raw_Data\\Raw_Simulation_Data_2014-03-24 17:21:20.545000.csv'
解決方案:
這個主要是文件名、路徑中含有反轉字符
,具體請參照Naming Files, Paths, and Namespaces,路徑中不要包含以下字符:
<
(less than)>
(greater than):
(colon)"
(double quote)/
(forward slash)\
(backslash)|
(vertical bar or pipe)?
(question mark)*
(asterisk)
兩種解決方式:
-
使用原生字符串
r'path\regionlog.txt'
-
去掉這些字符
#coding: utf-8 import re str1 = u'###現在開始,PM2.5小#米4 已經降到1299元! <a href="http://weibo.cn/sinaurl?f=w&u=http%3A%2F%2Ft.cn%2FRAJVndT&ep=D1Kpp8vLx%2C1749127163%2CD1Kpp8vLx%2C1749127163">http://t.cn/RAJVndT</a>' str2 = u'【設計之美,美到不可%%思議】彩虹五號\n電池,采用\日本Maxell 電池核芯,電量充足,而且是鹼性環保電池,無汞無鎘,每盒十節才 9.9元,送收納盒。小米網現貨 <a href="http://weibo.cn/sinaurl?f=w&u=http%3A%2F%2Ft.cn%2FRzlIR1U&ep=D1lQnDS5B%2C1749127163%2CD1lQnDS5B%2C1749127163">http://t.cn/RzlIR1U</a>' str3 = u'【小米電視3 新增55英寸,僅3999元】原裝LG/三星屏真4K,全金屬機身薄至9.9mm, 旗艦處理器,配MEMC運動圖像補償芯片,標配獨立音響。【小米盒子3】全新升級64位4K網絡機頂盒,標配小米藍牙語音體感遙控,僅299元。' str4 = u'小米生態鏈企業Ninebot CEO<a href="/n/%E9%AB%98%E7%A6%84%E5%B3%B0">@高祿峰</a> 告訴我,九號平衡車,截至現在預約量已突破40萬台。大家別忘了明天首發,現在預約 <a href="http://weibo.cn/sinaurl?f=w&u=http%3A%2F%2Ft.cn%2FRzlIR1U&ep=D20GP9Zio%2C1749127163%2CD20GP9Zio%2C1749127163">http://t.cn/RzlIR1U</a>' str5 = u'小米優惠多得數不清,力度也是空前的,我請同事整理一份完整的優惠清單供大家參考……' str6 = u'<a href=/n/E69D8EE5ADA6E5878C>@李學凌</a> 試玩九號平衡車第一次就搞定他說廣告詞應該改成零秒上手擠眼' str7 = u'原創】國產 5 P 聚會,人齊了就正式開始吧~(第二部)【37P】' pattern = re.compile('(<a href=\\S+</a>)|(@\\S+)') #content = weibo['content'].replace('http://', '!#$%&')\ # .split('//')[0].replace('!#$%&', 'http://')\ # .strip() #content = pattern.sub('', weibo['content']).split('//', 1)[0].strip() punct = set(u''':!),:;?]}¢'"、。〉》」』】〕〗〞︰︱︳/\ ﹐、#$%& ﹔﹕﹖﹗﹚﹜﹞!),:;?|}︴︶︸︺︼︾﹀﹂﹄﹏、~¢ 々‖•·ˇˉ―--′’”([{£¥'"‵〈《「『【〔〖([{£¥〝︵︷︹︻ ︽︿﹁﹃﹙﹛﹝({“‘-—_…''') # 對str/unicode filterpunt = lambda s: ''.join(filter(lambda x: x not in punct, s)) #print pattern.sub('', str6) print filterpunt(pattern.sub('', str2)).strip() print filterpunt(str7).strip()