简体   繁体   English

如何获取多个动态文本文件的内容并将其存储在一个文本文件和mysql-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?

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.

相关问题 读取多个文件夹并将多个文本文件内容合并到每个文件夹一个文件 - Python - Read in multiple folder and combine multiple text files contents to one file per folder - Python 如何在文本文件中追加数据并使用 python 将多个文本文件合并为一个文本文件? - How do I append the data in text files and combine multiple text files into one text file using python? 在一个文件中旋转多个文本文件的内容? - Rotate through the contents of multiple text files in one single file? 如何在Python中将多个文本文件合并为一个csv文件 - How to merge multiple text files into one csv file in Python 在python中将多个列表写入文本文件-2.7 - Write multiple lists to text file in python - 2.7 无法在python 2.7中获取文本文件输出 - Unable to get text file output in python 2.7 Python:如何读取多个文件夹中的所有文本文件内容并将其保存到一个excel文件中 - Python: How to read all text file contents in multiple folders and save them into one excel file 将多个文本文件合并为一个文本文件? - combine multiple text files into one text file? 如何让 python 只读取文本文件的内容一次 - How to get python to only read the contents of a text file once 如何在Sublime Text 3 Python API中获取文件的内容 - How to get contents of file in Sublime Text 3 Python API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM