[英]Reading Dictionaries from csv files
One of my cells in my csv has the following data:我的 csv 中的一个单元格具有以下数据:
'{{" Colour" : " Orange (OG)"," Open, post termination (Yes/No)" : " No"," Pack Cont." : " 1"," PART DESCRIPTION" : " BSWTOOL12-PL-OG"," Product Group" : " Hot Knives, Installation Tools & Accessories"," Short Description" : " Installation Tool, 1/2" Split Wrap Braided Sleeving, 1/pkg"},{" " : "None"," Bundle ∅ max." : " 12.7mm"," Diameter (D)" : " 12.7mm"," Nominal ∅" : " 12.7mm"," Nominal ∅" : " 0.5""," ∅ D" : " 0.50""},{" CE Certification" : " No"," CSA Certified" : " No"," Halogenfree" : " Yes"," Material" : " Plastic (PL)"," Specifications" : " "," UL Listed (US and Canada)" : " No"," UL Recognized (US and Canada)" : " No"},{" GTIN-13 / EAN" : " 4031026432168"," Packaging 1 - Weight (kg)" : " 0.014kg"},{" ETIM 6.0" : " EC001182"," ETIM 7.0 Key" : " EC001182"," UNSPSC Key" : " 39131602"}}'
I tried json.loads()
and ast.literal_eval()
for that cell but both of them throw an error.我为该单元尝试
json.loads()
和ast.literal_eval()
但它们都抛出了错误。
json.loads() throws the following: json.loads() 抛出以下内容:
JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
JSONDecodeError:期望用双引号括起来的属性名称:第 1 行第 2 列(字符 1)
ast.literal_eval() gives invalid syntax error. ast.literal_eval() 给出无效的语法错误。
PS: in the dictionary if you see an additional " character, that is to signify inches so technically the data can't be changed. PS:如果您在字典中看到额外的“字符,即表示英寸,因此从技术上讲数据无法更改。
I see a few issues right away:我马上看到了几个问题:
You have double braces {{
and }}
, which is not a valid JSON object (or Python dict).您有双括号
{{
和}}
,这不是有效的 JSON object (或 Python 字典)。
You have unescaped quotes in string literals, such as: " Installation Tool, 1/2" Split Wrap Braided Sleeving, 1/pkg"
您在字符串文字中有未转义的引号,例如:
" Installation Tool, 1/2" Split Wrap Braided Sleeving, 1/pkg"
The outer braces seem to denote a JSON array or Python list rather than a dictionary.外大括号似乎表示 JSON 数组或 Python 列表而不是字典。
Try taking the string and converting it to valid JSON to see what transformations you'll need to apply.尝试获取字符串并将其转换为有效的 JSON 以查看您需要应用哪些转换。
I suspect another thing you'll want to do is strip whitespace from dictionary keys (and possibly values too).我怀疑你想做的另一件事是从字典键(也可能是值)中去除空格。
I strongly recommend you do not eval the string, as this executes code, which can lead to security issues depending on how you intend to consume this data.我强烈建议您不要评估字符串,因为这会执行代码,这可能会导致安全问题,具体取决于您打算如何使用这些数据。
As mentioned, it's not a valid json.如前所述,它不是有效的 json。 Use this for comparsion and see if you can convert your csv or string to match.
使用它进行比较,看看您是否可以转换您的 csv 或字符串以匹配。 Then it'll work:
然后它会工作:
import json
jsonStr_error = '{{" Colour" : " Orange (OG)"," Open, post termination (Yes/No)" : " No"," Pack Cont." : " 1"," PART DESCRIPTION" : " BSWTOOL12-PL-OG"," Product Group" : " Hot Knives, Installation Tools & Accessories"," Short Description" : " Installation Tool, 1/2" Split Wrap Braided Sleeving, 1/pkg"},{" " : "None"," Bundle ∅ max." : " 12.7mm"," Diameter (D)" : " 12.7mm"," Nominal ∅" : " 12.7mm"," Nominal ∅" : " 0.5""," ∅ D" : " 0.50""},{" CE Certification" : " No"," CSA Certified" : " No"," Halogenfree" : " Yes"," Material" : " Plastic (PL)"," Specifications" : " "," UL Listed (US and Canada)" : " No"," UL Recognized (US and Canada)" : " No"},{" GTIN-13 / EAN" : " 4031026432168"," Packaging 1 - Weight (kg)" : " 0.014kg"},{" ETIM 6.0" : " EC001182"," ETIM 7.0 Key" : " EC001182"," UNSPSC Key" : " 39131602"}}'
jsonStr_works = r'[{" Colour" : " Orange (OG)"," Open, post termination (Yes/No)" : " No"," Pack Cont." : " 1"," PART DESCRIPTION" : " BSWTOOL12-PL-OG"," Product Group" : " Hot Knives, Installation Tools & Accessories"," Short Description" : " Installation Tool, 1/2\" Split Wrap Braided Sleeving, 1/pkg"},{" " : "None"," Bundle ∅ max." : " 12.7mm"," Diameter (D)" : " 12.7mm"," Nominal ∅" : " 12.7mm"," Nominal ∅" : " 0.5\""," ∅ D" : " 0.50\""},{" CE Certification" : " No"," CSA Certified" : " No"," Halogenfree" : " Yes"," Material" : " Plastic (PL)"," Specifications" : " "," UL Listed (US and Canada)" : " No"," UL Recognized (US and Canada)" : " No"},{" GTIN-13 / EAN" : " 4031026432168"," Packaging 1 - Weight (kg)" : " 0.014kg"},{" ETIM 6.0" : " EC001182"," ETIM 7.0 Key" : " EC001182"," UNSPSC Key" : " 39131602"}]'
jsonData = json.loads(jsonStr_works)
Output: Output:
print(jsonData[0])
{' Colour': ' Orange (OG)', ' Open, post termination (Yes/No)': ' No', ' Pack Cont.': ' 1', ' PART DESCRIPTION': ' BSWTOOL12-PL-OG', ' Product Group': ' Hot Knives, Installation Tools & Accessories', ' Short Description': ' Installation Tool, 1/2" Split Wrap Braided Sleeving, 1/pkg'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.