简体   繁体   English

将 Null 项目插入集合 Mongodb (Pymongo)

[英]Inserting Null items into a collection Mongodb (Pymongo)

Disclaimer: I'm a newbie when it comes to mongo..免责声明:我是 mongo 的新手..

So i have this data from a text-file that i am processing into "python dictionary" format so that it can be inserted into a collection i created using Pymongo.因此,我将这些数据从我正在处理的文本文件中转换为“python 字典”格式,以便可以将其插入到我使用 Pymongo 创建的集合中。

raw data changed to text, apologies... can be viewed here on pastebin Link to raw data text原始数据更改为文本,抱歉...可以在 pastebin 上查看链接到原始数据文本

And here is the formatted dictionary in python for insertion这是 python 中用于插入的格式化字典

[{'Poll_Name': 'ECU', 'Date': '2020-05-07', 'Sample_Size': '--', 'MoE': '--', 'Biden (D)': '46', 'Trump(R)': '43', 'Spread': 'Trump +3'}, {'Poll_Name': 'WRAL-TV', 'Date': '2020-04-23', 'Sample_Size': '580 LV', 'MoE': '5.5', 'Biden (D)': '45', 'Trump(R)': '50', 'Spread': 'Biden +5'}, {'Poll_Name': 'PPP (D)', 'Date': '2020-04-14', 'Sample_Size': '1318 RV', 'MoE': '2.7', 'Biden (D)': '47', 'Trump(R)': '48', 'Spread': 'Biden +1'}, {'Poll_Name': 'Civitas', 'Date': '2020-04-05', 'Sample_Size': '500 LV', 'MoE': '4.4', 'Biden (D)': '49', 'Trump(R)': '42', 'Spread': 'Trump +7'}]

I have all the dictionary data inserted into an array and the i plan to do an insertmany() with it.我已将所有字典数据插入到一个数组中,我打算用它做一个 insertmany()。

Here is the code i have so far for the exporting of this data in dictionary format这是我到目前为止以字典格式导出此数据的代码

def export_Data(filename):
export_List = [] #list that will contain the dictionary values of the data
key_List = ["Poll_Name", "Date", "Sample_Size", "MoE", "Biden (D)", "Trump(R)", "Spread"] #list of keys for each value
count = 0 
temp_List = []
with(open(filename, "r")) as infile: #opening the file of raw data
    for line in infile:
        count += 1
        temp_List.append(line.strip("\n")) #i add each line of infile to this temporary list 
        if count % len(key_List) == 0: #when 7 items are added
            temp_dict = {} #create a temporary dictionary 
            for key, line in zip(key_List, temp_List): #fill in dictionary key values..
                temp_dict[key] = line
            temp_List = [] # resetting the temporary dictionary
            export_List.append(temp_dict) #appending dictionary to final list
print(export_List)
#export the list later once i get properly formatted..

Now as you can see some of the entries found in both the text file and the dictionary sample are seen as "--", these are suppost to represent empty/null values.现在您可以看到在文本文件和字典示例中找到的一些条目被视为“--”,这些条目假定代表空/空值。 I want to insert values like these as null and not "--" into my database so as to avoid having to do a mass update query in mongo, i feel like it might make the data cleaning/export process simpler and faster.我想在我的数据库中插入像 null 而不是“--”这样的值,以避免在 mongo 中进行大规模更新查询,我觉得它可能会使数据清理/导出过程更简单、更快。 Is there any way i can change these values so they can be inserted as null instead of "--" Would appreciate any solutions, I know there is probably a simple answer.有什么办法可以更改这些值,以便可以将它们插入为 null 而不是“--” 将不胜感激任何解决方案,我知道可能有一个简单的答案。 but this newbie would appreciate some clarification.但是这个新手希望得到一些澄清。

You can explicitly replace "--" with any value of your choice in that loop:您可以在该循环中将“--”显式替换为您选择的任何值:

instead of代替

        temp_List.append(line.strip("\n")) 
        temp_str = line.strip("\n")
        temp_list.append(temp_str if (temp_str != "--") else "")

If you would rather use null or undefined or "Not Provided" or whatever, just replace "" with what you'd like to use.如果您更愿意使用nullundefined或“未提供”或其他任何内容,只需将""替换为您想要使用的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM