2017-10-04 98 views
0

我有一個CSV文件Decoded.csv大熊貓無法細胞

Query,Doc,article_id,data_source 
5000,how to get rid of serve burn acne,1 Rose water and sandalwood: Make a paste of rose water and sandalwood and gently apply it on your acne scars. 
2 Leave the paste on your skin overnight then wash it with cold water the next morning. 
3 Do this regularly together with other natural treatments for acne scars to get rid of the scars as quickly as possible.,459,random 
5001,what is hypospadia,A birth defect of the male urethra.,409,dummy 
5002,difference between alimentary canal and accessory organs,The alimentary canal is the tube going from the mouth to the anus. The accessory organs are the organs located along that canal which produce enzymes to aid the digestion process.,461,nytimes 

而且有3個查詢5000,5001 查詢5000有具有多個一督值內解析多行的CSV線路,這是熊貓混淆。 (1份玫瑰水和檀香:。讓玫瑰水和檀香糊狀,輕輕把它在你的痤瘡疤痕 2保留貼在皮膚上過夜,然後第二天早上用冷水沖洗 3裏這樣做經常在一起與痤瘡疤痕等自然療法得到儘快擺脫疤痕越好)

我的Python代碼是根據

def main(): 
    import pandas as pd 
    dataframe = pd.read_csv("Decoded.csv") 
    queries, docs = dataframe['Query'], dataframe['Doc'] 
    for idx in range(len(queries)): 
     print("idx: ", idx, " ", queries[idx], " <-> ", docs[idx]) 
     query_doc_appended = (queries[idx] + " " + docs[idx]) 
    print(query_doc_appended) 

if __name__ == '__main__': 
    main() 

而失敗。請指出我如何擺脫換行符,以便Query 5000具有Doc的完整語句集。

+0

任何錯誤消息?你的數據文件看起來像什麼?不清楚。 –

+0

數據文件在這個問題本身Decoded.csv 提供查詢,DOC,article_id的,DATA_SOURCE ......而 誤差 回溯(最近通話最後一個): 線53,在 的main() 線49 , query_doc_appended =(queries [idx] +「」+ docs [idx]) TypeError:不支持的操作數類型爲+:'float'和'str' idx:0如何擺脫服務燒痤瘡<-> 1玫瑰水和檀香:使成糊狀的玫瑰水和檀香,輕輕把它在你的痤瘡疤痕。 IDX:1囡楠<->和 – khangaroth

+0

當你運行這個程序,你得到了什麼錯誤訊息? –

回答

0

您的查詢5001行中包含太多字段,使其具有5列而不是其他行具有4個字段。

5001,what is hypospadia,A birth defect of the male urethra.,409,dummy 

您可以在Decoded.csv中將您的Doc內容加雙引號以解決此問題。

0

2問題:

  • 以允許多行字段,該字段數據必須被包括在雙引號。
  • 您的字段數據中也有逗號。

因此,CSV應該是這樣的:

Query,Doc,article_id,data_source 
5000,"how to get rid of serve burn acne,1 Rose water and sandalwood: Make a paste of rose water and sandalwood and gently apply it on your acne scars. 
2 Leave the paste on your skin overnight then wash it with cold water the next morning. 
3 Do this regularly together with other natural treatments for acne scars to get rid of the scars as quickly as possible.",459,random 
5001,"what is hypospadia,A birth defect of the male urethra.",409,dummy 
5002,"difference between alimentary canal and accessory organs,The alimentary canal is the tube going from the mouth to the anus. The accessory organs are the organs located along that canal which produce enzymes to aid the digestion process.",461,nytimes 

的情況下有這些領域內的雙引號,它們必須與另一個雙引號進行轉義。

+0

謝謝。這很有幫助。 – khangaroth