我有一個Python中的字典(最新版本)的問題。 這裏是我的字典: [ {dict1} , {dict2} , ... ]
所有字典是類似於:問題Python寫作 - 讀取字典的CSV
{'Date': '2016-10-17',
'Message_body': ' Version française BUSINESS EVENTS - SPRING 2016 April 5: YESS EVENT ON SCALING UP Robin Bonsey, Hystra Consultant, will discuss business solutions to the predicament of small holder farmer',
'Sender': '[email protected]',
'Subject': 'Fwd: Inclusive business events - spring 2016'}
。
根據Python,每個值(type(dict1['Message_body'])
)的'類型'是「str」。 我的問題是將此詞典詞典轉換爲CSV文件(使用密鑰'Date' , 'Message_body' , 'Sender', 'Subject'
)。 這裏是我的代碼:
def export_dict_list_to_csv(data, filename):
with open(filename, 'w',encoding='utf-8',newline='') as f:
# Assuming that all dictionaries in the list have the same keys.
headers = sorted([k for k, v in data[0].items()])
csv_data = [headers]
for d in data:
csv_data.append([d[h] for h in headers])
writer = csv.writer(f)
writer.writerows(csv_data)
export_dict_list_to_csv(final_list, 'chili.csv')
它工作得很好,但錯字很奇怪。 例如,在.csv中,我有「Chaque moi voudraitêtrele tyran de tous les autres»dit Pascal dans lesPensées」,而不是「Chaque moi voudraitêtretyran de tous les autresàdit Pascal dans lesPensées 」。在「str」形式中,我有「良好的錯字」,但在.csv中,它不是很好的錯字(我不知道爲什麼)。如果「讀取」CSV文件恢復「str」的良好初始輸入錯誤,則此問題並不重要。
但我沒有成功讀取正確的CSV創建... 我想:
with open('chili.csv', 'r') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',')
for row in spamreader:
print (row)
,我得到錯誤「的UnicodeDecodeError:‘ASCII’編解碼器不能解碼位置字節0xc3 1087:序數不在範圍內(128)」
,我嘗試:
with open('/Users/Marco/HandB/Gmail/chili.csv', 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',')
for row in spamreader:
print (row)
錯誤:(?你以文本模式打開文件),迭代器應該返回字符串,而不是字節
所以我有2個問題: 1)我寫CSV文件的方式是一種好方法嗎?爲什麼我在CSV上有一個奇怪的錯字?
2)如何讀取之前創建的CSV? 我在互聯網上搜索了幾個小時,但我沒有找到任何特別的東西來幫助我解決這個問題。特別是,我不太清楚圍繞「編碼」問題的所有問題,我只知道字典中的值是str類型,我認爲它們是UTF-8格式。 下面是代碼:(我清理「數據」,從GMAIL API收到)
mssg_parts = payld['parts'] # fetching the message parts
part_one = mssg_parts[0] # fetching first element of the part
part_body = part_one['body'] # fetching body of the message
part_data = part_body['data'] # fetching data from the body
clean_one = part_data.replace("-","+") # decoding from Base64 to UTF-8
clean_one = clean_one.replace("_","/") # decoding from Base64 to UTF-8
clean_two = base64.b64decode (bytes(clean_one, 'UTF-8')) # decoding from Base64 to UTF-8
soup = BeautifulSoup(clean_two , "lxml")
soup = BeautifulSoup(clean_two, "html")
soup.get_text()
mssg_body = soup.body()
# mssg_body is a readible form of message body
# depending on the end user's requirements, it can be further cleaned
# using regex, beautiful soup, or any other method
temp_dict['Message_body'] = mssg_body
我寫下這給我提供了「新的訊息」的一部分,因爲它可以幫助你理解的格式的代碼消息及其轉換爲CSV文件。
非常感謝! :)
看來你是在python3上。嘗試像這樣設置編碼:'open('/ Users/Marco/HandB/Gmail/chili.csv','r',encoding ='utf-8')as csvfile:' –
謝謝coldspeed for your answer !你說得對,實際上它和「編碼」一起工作,錯字很好! ^^最後一個問題:我如何重新創建一個「字典」這個「閱讀CSV」?謝謝:) – Eliot
已經發布了一個答案。如果您覺得有幫助,請考慮將其標記爲已接受。 –