简体   繁体   English

我在将csv文件写入MongoDB时遇到问题

[英]I have issues in writing csv files into MongoDB

I am trying to write all csv files in a folder into the MongoDB collection. 我试图将一个文件夹中的所有csv文件写入MongoDB集合。 but I keep getting error as file not found when the file exists in the directory. 但是当目录中存在文件时,由于找不到文件,我不断收到错误消息。

output_files = [file for file in os.listdir(dir_path) if file.endswith(".csv")]
print("Files in the folder:", output_files)

#create mongoclient object    
client = MongoClient()

#get the database and list the collections
db = client.senci
collection_names_list = db.list_collection_names()
print("MongoDB collections:", collection_names_list)

for file in output_files:
    collection = "senci_" + file[:-4]

    if collection in collection_names_list:
        #print("File exists. Documents in this collection will be deleted.")
        col = db[collection].delete_many({})
        print(col.deleted_count, " documents deleted from", collection)

        #write new data
        df = pd.read_csv(file, engine='python', delimiter=',')
        records_ = df.to_dict(orient = 'records')
        db[collection].insert_many(records_)
        print("Collection updated.\n")

Below is the error i get. 下面是我得到的错误。 First 3 lines are to ensure that csv exists. 前3行是为了确保csv存在。

Files in the folder: ['groceries.csv']
MongoDB collections: ['senci_adult_diapers', 'senci_health_supplements', 'senci_groceries', 'senci_mobility_aids']
0  documents deleted from senci_groceries
Traceback (most recent call last):
  File "tt.py", line 27, in <module>
    df = pd.read_csv(file, engine='python', delimiter=',')
  File "C:\Users\USER\Anaconda3\envs\goki\lib\site-packages\pandas\io\parsers.py", line 702, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\USER\Anaconda3\envs\goki\lib\site-packages\pandas\io\parsers.py", line 429, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "C:\Users\USER\Anaconda3\envs\goki\lib\site-packages\pandas\io\parsers.py", line 895, in __init__
    self._make_engine(self.engine)
  File "C:\Users\USER\Anaconda3\envs\goki\lib\site-packages\pandas\io\parsers.py", line 1132, in _make_engine
    self._engine = klass(self.f, **self.options)
  File "C:\Users\USER\Anaconda3\envs\goki\lib\site-packages\pandas\io\parsers.py", line 2225, in __init__
    memory_map=self.memory_map)
  File "C:\Users\USER\Anaconda3\envs\goki\lib\site-packages\pandas\io\common.py", line 427, in _get_handle
    f = open(path_or_buf, mode, errors='replace', newline="")
FileNotFoundError: [Errno 2] No such file or directory: 'groceries.csv'

You are passing the name of the file, you need to pass the full path . 您传递文件的名称 ,你需要传递的完整路径 The os.path.join function can be used to combine a directory path and a file name os.path.join函数可用于组合目录路径和文件名

import os.path

# Gets *names*
output_files = [file for file in os.listdir(dir_path) if file.endswith(".csv")]

...    

for file in output_files:

    # Get full path
    path = os.path.join(dirpath, file)

    # write new data
    df = pd.read_csv(path, engine='python', delimiter=',')

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

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