[英]Import CSV row into a dict?
之前曾問過這個問題,但在我的情況下,沒有答案適用。
我正在讀取一個csv文件:
with codecs.open('./products.csv', 'r', encoding="utf-8") as _filehandler:
csv_file_reader = csv.DictReader(_filehandler)
for row in csv_file_reader:
在我的CSV文件中,我在一行中包含以下內容:
'#custom_shrink_wrapping': '700', '#custom_green_paper': '338'
我的目標是將此添加到字典中。 上一欄是一欄。
這是數據示例:
item,parse_dropdowns,fixed_dropdowns_values,links
postcards,"#quantity, #paper, #size, #color, #turnaround, #coating","‘#custom_finishing': '497', '#custom_shrink_wrapping': '700', '#custom_green_paper': '338'",http://www.example.com/products/postcards
flyers,"#quantity, #paper, #size, #color, #turnaround, #coating, #folding","‘#custom_green_paper': '338', '#custom_hole_punch': '204', '#custom_shrink_wrapping': '700'",http://www.example.com/products/brochures
brochures,"#quantity, #paper, #size, #color, #turnaround, #coating, #folding","‘#custom_green_paper': '338', '#custom_hole_punch': '204', '#custom_shrink_wrapping': '700'",http://www.example.com/products/brochures
business cards,"#quantity, #paper, #size, #color, #turnaround, #coating","‘#custom_green_paper': '338', '#custom_shrink_wrapping': '700', '#versionCustomerPulldown': '1'",http://www.example.com/products/businesscards
bookmarks,"#quantity, #paper, #size, #color, #turnaround, #coating","‘#custom_finishing': '497', '#custom_shrink_wrapping': '700', '#custom_green_paper': '338'",http://www.example.com/products/bookmarks
calendars,"#quantity, #paper, #size, #color, #turnaround, #page, #coating","‘#custom_green_paper': '338', '#custom_finishing': '13356', '#custom_hole_punch': '205', '#custom_shrink_wrapping': '700'",http://www.example.com/products/calendars
最終目標是做到這一點:
{'#custom_shrink_wrapping': '700', '#custom_green_paper': '338'}
我以為這樣做很簡單:
dropdownValuesCsv = dict()
dropdownValuesCsv.append( row['fixed_dropdowns_values'] )
那失敗了。 然后我嘗試了這個:
dropdownValuesCsv = dict()
dropdownValuesCsv.update( row['fixed_dropdowns_values'] )
這產生了這個錯誤:
ValueError: dictionary update sequence element #0 has length 1; 2 is required
然后我嘗試了這個:
dropdownValuesCsv = { row['fixed_dropdowns_values'] }
但這會產生一個給出錯誤的集合,而不是我想要的。
我認為您的問題中只有一些混淆點可以為您解決。
首先,我不清楚您說CSV的一列值為
'#custom_shrink_wrapping': '700', '#custom_green_paper': '338'
,因為其中包含逗號(並且您未指定不同的分隔符),因此應該是兩列?
我將用另一個分隔符(例如,分號)代替的假設來回答您的問題。
編輯:此后已通過添加的屏幕截圖進行了澄清。
我相信您的主要問題是您沒有考慮csv.DictReader
的輸出類型。 當它解析您的csv時,它將把鍵映射到字符串,因此當您嘗試用其輸出更新字典時,您不會得到想要的多個值(僅一個字符串,這意味着這些值)。 我們可以通過解析字符串來解決這個問題。
這是我的工作示例:
tmp.csv
fixed_dropdown_values, other
"'#custom_shrink_wrapping': '700', '#custom_green_paper': '338'", 'other': 'other'
test.py
import csv
dropdownValuesCSV = {}
with open('./tmp.csv') as file_handler:
reader = csv.DictReader(file_handler)
for row in reader:
# We split multiple key-value pairs by comma
for mapping in row['fixed_dropdown_values'].split(','):
# We do an additional split on a colon to differentiate keys and values
# ... and do some extra cleanup to remove extra spaces and quotation marks
key, val = [s.strip(' ').replace("'", '') for s in mapping.split(':')]
dropdownValuesCSV[key] = val
print dropdownValuesCSV
# dropdownValuesCSV is:
# {'#custom_green_paper': '338', '#custom_shrink_wrapping': '700'}
希望能有所幫助。
編輯:修改后的代碼以使用ast.literal_eval()
而不是更強大(且可能更危險)的內置eval()
因為它更安全,因為這就是這里所需要的全部。
請注意,為了進行測試,我使用添加到問題中的數據創建了一個products.csv
文件-盡管我不得不將其中'
字符更改為'
字符,導致包含此字符(因為否則讀取文件會導致codec
錯誤) :
item,parse_dropdowns,fixed_dropdowns_values,links
postcards,"#quantity, #paper, #size, #color, #turnaround, #coating","'#custom_finishing': '497', '#custom_shrink_wrapping': '700', '#custom_green_paper': '338'",http://www.example.com/products/postcards
flyers,"#quantity, #paper, #size, #color, #turnaround, #coating, #folding","'#custom_green_paper': '338', '#custom_hole_punch': '204', '#custom_shrink_wrapping': '700'",http://www.example.com/products/brochures
brochures,"#quantity, #paper, #size, #color, #turnaround, #coating, #folding","'#custom_green_paper': '338', '#custom_hole_punch': '204', '#custom_shrink_wrapping': '700'",http://www.example.com/products/brochures
business cards,"#quantity, #paper, #size, #color, #turnaround, #coating","'#custom_green_paper': '338', '#custom_shrink_wrapping': '700', '#versionCustomerPulldown': '1'",http://www.example.com/products/businesscards
bookmarks,"#quantity, #paper, #size, #color, #turnaround, #coating","'#custom_finishing': '497', '#custom_shrink_wrapping': '700', '#custom_green_paper': '338'",http://www.example.com/products/bookmarks
calendars,"#quantity, #paper, #size, #color, #turnaround, #page, #coating","'#custom_green_paper': '338', '#custom_finishing': '13356', '#custom_hole_punch': '205', '#custom_shrink_wrapping': '700'",http://www.example.com/products/calendars
這是可以閱讀並執行所需操作的代碼:
import ast
import codecs
import csv
COL_NAME = 'fixed_dropdowns_values' # Column of interest.
with codecs.open('./products.csv', 'rb', encoding="utf-8") as _filehandler:
csv_file_reader = csv.DictReader(_filehandler)
for row in csv_file_reader:
dropdownValuesCsv = ast.literal_eval('{' + row[COL_NAME] + '}')
print(dropdownValuesCsv)
這是它在讀取(已修改的)輸入時創建和打印的字典:
{'#custom_finishing': '497', '#custom_green_paper': '338', '#custom_shrink_wrapping': '700'}
{'#custom_hole_punch': '204', '#custom_shrink_wrapping': '700', '#custom_green_paper': '338'}
{'#custom_hole_punch': '204', '#custom_shrink_wrapping': '700', '#custom_green_paper': '338'}
{'#versionCustomerPulldown': '1', '#custom_shrink_wrapping': '700', '#custom_green_paper': '338'}
{'#custom_finishing': '497', '#custom_green_paper': '338', '#custom_shrink_wrapping': '700'}
{'#custom_hole_punch': '205', '#custom_finishing': '13356', '#custom_shrink_wrapping': '700', '#custom_green_paper': '338'}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.