我目前正在开展一些数据分析工作,而且我在数据预处理方面遇到了一些麻烦。
我编译了一个文本文件夹,文本文件的名称是文本文件对应的日期。我最初能够将所有文本文件附加到一个文档,但我想使用字典以便有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附加到文件中的任何想法?
答案 0 :(得分:3)
有很多问题,但导致问题的是你要打开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
)dict
和file
finalv2.txt
,仅在一个地方定义filename
未定义两次,一次作为输出文件,然后再次作为输入文件。相反,有out_file
和in_file
append_record
函数