簡體   English   中英

使用字典Python附加多個文本文件

[英]Appending Multiple Text Files using Dictionaries Python

我目前正在從事一些數據分析工作,並且在數據預處理方面遇到了一些麻煩。

我已經編譯了一個文本文件文件夾,該文本文件的名稱為該文本文件對應的日期。 我本來可以將所有文本文件追加到一個文檔中,但是我想使用字典來具有2個屬性,即文件名(也包括日期)和文本文件中的內容。

這是代碼:

import json
import os
import math

# Define output filename
OutputFilename = 'finalv2.txt'

# Define path to input and output files
InputPath  = 'C:/Users/Mike/Desktop/MonthlyOil/TextFiles'
OutputPath = 'C:/Users/Mike/Desktop/MonthlyOil/'

# Convert forward/backward slashes
InputPath  = os.path.normpath(InputPath)
OutputPath = os.path.normpath(OutputPath)

# Define output file and open for writing
filename = os.path.join(OutputPath,OutputFilename)
file_out = open(filename, 'w')
print ("Output file opened")

size = math.inf

def append_record(record):
    with open('finalv2.txt', 'a') as f:
        json.dump(record, f)
        f.write(json.dumps(record))

# Loop through each file in input directory
    for file in os.listdir(InputPath):
    # Define full filename
    filename = os.path.join(InputPath,file)
    if os.path.isfile(filename):
        print ("  Adding :" + file)
        file_in = open(filename, 'r')
        content = file_in.read()
        dict = {'filename':filename,'content':content}
        print ("dict['filename']: ", dict['filename'] )     
        append_record(dict)    
        file_in.close()


# Close output file
file_out.close()
print ("Output file closed")

我遇到的問題是它不會附加我的文件,我在其中一行測試dict是否包含任何內容,是否包含它,我已經測試了內容和文件名。

有什么想法我想要將dict附加到文件嗎?

有很多問題,但是引起問題的一個原因是您兩次打開finalv2.txt 一次使用w模式(對此不執行任何操作),再次在append_record()內部,這次使用a模式。

考慮以下:

import json
import os
import math

# Define output filename
OutputFilename = 'finalv2.txt'

# Define path to input and output files
InputPath  = 'C:/Users/Mike/Desktop/MonthlyOil/TextFiles'
OutputPath = 'C:/Users/Mike/Desktop/MonthlyOil/'

# Convert forward/backward slashes
InputPath  = os.path.normpath(InputPath)
OutputPath = os.path.normpath(OutputPath)

# Define output file
out_file = os.path.join(OutputPath,OutputFilename)

size = None

def append_record(fn, record):
    with open(fn, 'a') as f:
        json.dump(record, f)
        #f.write(json.dumps(record))

# Loop through each file in input directory
for fn in os.listdir(InputPath):
    # Define full filename
    in_file = os.path.join(InputPath,fn)
    if os.path.isfile(in_file):
        print("  Adding: " + fn)
        with open(in_file, 'r') as file_in:
            content = file_in.read()
            d = {'filename':in_file, 'content':content}
            print("d['filename']: ", d['filename'] )
            append_record(out_file, d)

哪個按預期工作。

這里:

  • 文件未明確打開和關閉,而是由上下文管理器( with )管理
  • 不再有名為dictfile變量
  • finalv2.txt在一個位置和一個位置定義finalv2.txt
  • filename沒有兩次定義,一次是輸出文件,另一次是輸入文件。 相反,有out_filein_file
  • 您將輸出文件名傳遞給您的append_record函數
  • 您不會(嘗試)將json附加兩次-只能附加一次(您可以選擇自己喜歡的方法,它們都可以工作)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM