简体   繁体   English

从一个文件读取到另一个

[英]Reading from one file to another

I'm sure it's something simple that I'm overlooking. 我敢肯定,这是我所忽略的简单事情。 I've done this kind of operation before. 我以前做过这种操作。 I'm reading from htmlfile, which is my template file, and writing to htmlfile2 which is my actual report. 我正在读取htmlfile(这是我的模板文件),然后写入htmlfile2(这是我的实际报告)。 Before anyone freaks out, the HTML isn't being used on a website or anything, it's just being used as a convenient means to display report data. 在任何人吓坏之前,HTML并没有在网站上或任何东西上使用,它只是用作显示报告数据的便捷方式。 I found rgraph, a tool for creating HTML5 graphs, and thought, "Wow, this is perfect! I don't even have to code a GUI now and I can easily save my report results!" 我找到了用于创建HTML5图形的工具rgraph,然后想到:“哇,这太完美了!我什至不必现在编写GUI,而且我可以轻松保存报告结果!”

My htmlfile has several lines with comments that contain text that tells my program that said line needs replaced in htmlfile2. 我的htmlfile有几行带有注释的行,这些注释包含告诉我的程序所说的行需要在htmlfile2中替换的文本。 If I use print statements to check which loops are executing, the proper loops execute every time. 如果我使用print语句检查正在执行的循环,则每次都会执行正确的循环。 For some reason the text just never changes. 由于某种原因,文本永远不会改变。 It's as if the write function takes place, but doesn't work, it fails silently. 好像发生了写入功能,但不起作用,它无声地失败了。 The value that is in the template is NOT in the python code anywhere, so the write is simply not doing it's job, even though the loop has been entered. 模板中的值不在任何地方的python代码中,因此即使已进入循环,写入操作也无法完成。 I've tried a few different if conditions, just to see if there would be any change, hence the startswith vs the ==, etc. Help??? 我尝试了几种不同的if条件,只是看是否会有任何变化,因此,startswith vs ==等。帮助?

def editHTML(self, searchT, replaceT):
    if os.path.isfile('C:/MetReports/report.html'):
        with open('C:/MetReports/report.html') as htmlfile:
            with open('C:/MetReports/report2.html', 'w') as htmlfile2:
                for line in htmlfile:
                    if searchT in line:
                        if searchT.startswith('REP1'):
                            tester = "\t\t\tvar meter = new RGraph.Meter('cvs', 0,100, " + str(replaceT) + ") //REP1\n"
                            htmlfile2.write(tester)
                        elif searchT == 'REP2':
                            tester2 = "\t\t\tvar meter = new RGraph.Meter('cvs2', 0,100, " + str(replaceT) + ") //REP2\n"
                            htmlfile2.write(tester2)
                        elif searchT == 'REP3':
                            htmlfile2.write("\t\t\tvar meter = new RGraph.Meter('cvs3', 0,1000, " + str(replaceT) + ") //REP3\n")
                        elif searchT == 'REP4':
                            htmlfile2.write("\t\t\tvar meter = new RGraph.Meter('cvs4', 0,30, " + str(replaceT) + ") //REP4\n")
                        elif searchT == 'REP5':
                            htmlfile2.write("\t\t\tvar meter = new RGraph.Meter('cvs5', 0,30, " + str(replaceT) + ") //REP5\n")
                    elif searchT not in line:
                        htmlfile2.write(line)

Edit for clarification: 编辑以澄清:

Example lines from htmlfile: htmlfile中的示例行:

<canvas id="cvs" width="400" height="250" style="border: 1px solid #ccc; border-radius: 15px">[No canvas support]</canvas>
        <script>
    $(document).ready(function ()
    {
        var meter = new RGraph.Meter('cvs', 0,100, 25) //REP1
            .set('angles.start', RGraph.PI - 0.5)
            .set('angles.end', RGraph.TWOPI + 0.5)

The 25 is the number that should change when the loop is entered and the searchT variable is subbed in. So an example run would look like: 25是在进入循环且将searchT变量加入其中时应更改的数字。因此,示例运行如下所示:

editHTML('REP1', 30)

which should change only one line above, replacing the 25 with the 30. 应该只改变上面的一行,用25代替25。

EDIT 2: 编辑2:

It is as I thought, the text is being overwritten by the other if statement running when it shouldn't be. 就像我想的那样,当不应该运行该if语句时,该文本将被另一个if语句覆盖。 I placed "exit(0) after one of the writes and checked my file and the information in it was correct for that value alone.. 我在其中一次写入之后放置了“ exit(0)”,并检查了文件,并且其中的信息仅对于该值是正确的。

EDIT 3: 编辑3:

Solution in comments. 评论中的解决方案。 For anyone who ever has a similar issue in the future, always remember, debug, debug, debug! 对于将来遇到类似问题的任何人,请始终记住,调试,调试,调试! Step into your processes one by one and you'll find your issue. 一步一步地进入流程,您会发现问题。 In my case, I called a function without realizing that it would overwrite my output file with the contents of the input file. 就我而言,我调用一个函数时并未意识到它会用输入文件的内容覆盖我的输出文件。

In order to solve my issue I had to look at two things: loop structure, and process. 为了解决我的问题,我不得不看两件事:循环结构和过程。 My main function creates an object which has the function editHTML, in my case when I called editHTML multiple times, since I was reading from a static file (a template if you will) and searching for too specific criteria, I ended up overwriting lines. 我的主要功能创建了一个具有editHTML功能的对象,在本例中,当我多次调用editHTML时,由于我是从静态文件(如果需要的话,是模板)中读取内容并搜索过于具体的条件,因此我最终覆盖了行。 I have since changed the entire structure to make it simpler. 从那以后,我更改了整个结构以使其更简单。 Now, editHTML is only called a single time and instead of having to focus on how many times I needed this function to be called, and what that would do to my text, the function is called once, does all the work I need it to do in that single run, and resets itself should the user choose to run it again. 现在,editHTML仅被调用一次,而不必专注于需要多少次调用此函数,而对文本的处理将被调用一次,完成我需要的所有工作在一次运行中执行,并在用户选择再次运行时重置自身。

Now it just needs optimized. 现在,只需要优化即可。 Here is the new editHTML code, it is now passed a list (as opposed to two strings) as it's sole argument. 这是新的editHTML代码,现在将它作为唯一参数传递给列表(而不是两个字符串)。

   def editHTML(self, replaceT):
    if os.path.isfile('C:/MetReports/report.html'):
        with open('C:/MetReports/report.html') as htmlfile:
            with open('C:/MetReports/report2.html', 'w') as htmlfile2:
                fixer = '//REP'
                count = 0
                for line in htmlfile:
                    if line.find(fixer) == -1:
                        htmlfile2.write(line)
                    else:
                        if count == 0:
                            print("running 1")
                            htmlfile2.write("\t\t\tvar meter = new RGraph.Meter('cvs', 0,100, " + str(replaceT[0]) + ") //REP1\n")
                        elif count == 1:
                            print("running 2")
                            htmlfile2.write("\t\t\tvar meter = new RGraph.Meter('cvs2', 0,100, " + str(replaceT[1]) + ") //REP2\n")
                        elif count == 2:
                            print("running 3")
                            htmlfile2.write("\t\t\tvar meter = new RGraph.Meter('cvs3', 0,1000, " + str(replaceT[2]) + ") //REP3\n")
                        elif count == 3:
                            print("running 4")
                            htmlfile2.write("\t\t\tvar meter = new RGraph.Meter('cvs4', 0,30, " + str(replaceT[3]) + ") //REP4\n")
                        elif count == 4:
                            print("running 5")
                            htmlfile2.write("\t\t\tvar meter = new RGraph.Meter('cvs5', 0,30, " + str(replaceT[4]) + ") //REP5\n")
                        count += 1

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

相关问题 将凭据从一个文件读入另一个文件 - Reading credentials from one file into another 从一个文件读取并在Python中写入另一个文件 - Reading from one file and writing into another in Python 从一个文件中读取单词并将它们grep到另一个文件中 - Reading words from one file and grep them in another file 从一个文件中读取以下文本并将其写入另一个文件 - Reading the below text from one file and write it to another file 使用来自另一个文件的值从一个文件中读取特定行 - reading a specific line from one file using a value from another 从一个文件读取性能,执行一个动作并写入另一个文件的性能折衷 - Performance Tradeoff Reading From One File, Perfoming An Action and Writing To Another File 使用Python从一个文件夹中读取excel文件并输出到另一个文件夹中的csv文件时收到PermissionError? - Receiving PermissionError while reading excel file from one folder and outputting to csv file in another folder using Python? Python,一一读取文件中的行 - Python, reading lines from a file one by one 从一个文件读取并写入另一个文件 - Reading from a file and writing to another file 将文件从一个模型的FileField复制到另一个模型的FileField而不读取它 - Copy a File from one Model's FileField to another Model's FileField without reading it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM