[英]How to get the contents of multiple dynamic text files and store it in one text file and in mysql - Python 2.7?
I have multiple text file in my import folder where, I want to read all of the lines for each text file and store it in one text file only. 我的导入文件夹中有多个文本文件,我想读取每个文本文件的所有行并将其仅存储在一个文本文件中。 I am just going to test if all lines are read by python and eventually i will store it in mysql database.
我只是要测试是否所有行都被python读取,最终我会将其存储在mysql数据库中。 How do I do that using fileinput?
如何使用fileinput做到这一点? i cannot seem to start, my files are dynamic: here is my code:
我似乎无法启动,我的文件是动态的:这是我的代码:
for i in range(1, 4):
dateNow = datetime.datetime.today().date()
strNow = dateNow.strftime('%Y%m%d') + ".Dat"
cstrNow = pathA + str(i) + "\\" + strNow
dateYesterday = datetime.datetime.today().date() - timedelta(days = 1)
strYesterday = dateYesterday.strftime('%Y%m%d') + ".Dat"
cstrYesterday = pathA + str(i) + "\\" + strYesterday
try:
with open(cstrNow):
shutil.copy(cstrNow, pathImport + "\A" + str(i) + "_" + strNow)
pathNow = pathImport + "\A" + str(i) + "_" + strNow
except IOError:
print "No Dat file: " + pathImport + "\A" + str(i) + "_" + strNow
try:
with open(cstrYesterday):
shutil.copy(cstrYesterday, pathImport + "\A" + str(i) + "_" + strYesterday)
pathYesterday = pathImport + "\A" + str(i) + "_" + strYesterday
except IOError:
print "No Dat file: " + pathImport + "\A" + str(i) + "_" + strYesterday
for line in fileinput.input(pathNow):
print line
for line in fileinput.input(pathYesterday):
print line
I tested using this one, but it fails.. 我使用此工具进行了测试,但失败了。
with fileinput.input(files=(pathNow, pathYesterday)) as f:
for line in f:
//HOW TO OUTPUT TO TEXT FILE?
For now i dont have a problem for mysql, but if you could point me to a good tutorial, for now i read this one, seems to be fine: http://www.jeremymorgan.com/tutorials/python-tutorials/how-to-connect-to-mysql-with-python/ 目前,我对mysql没有任何问题,但是如果您可以为我提供一个不错的教程,那么现在阅读此教程似乎还不错: http : //www.jeremymorgan.com/tutorials/python-tutorials/how使用python /连接到MySQL /
edit: I am using python 2.7, How to get the contents of multiple dynamic text files and store it in one text file and in mysql? 编辑:我正在使用python 2.7,如何获取多个动态文本文件的内容并将其存储在一个文本文件和mysql中?
I hope this will help someone... 我希望这会帮助某人...
# READ ALL DAT FILE IN PATHIMPORT AND COMBINE INTO ONE TEXT FILE
strToday = dateToday()
read_files = glob.glob(pathImport + "\\" + "*.Dat")
with open("C:/swipeimport/" + "M_" + strToday + ".txt", "wb") as outfile:
for x in read_files:
with open(x, "rb") as infile:
outfile.write(infile.read())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.