简体   繁体   English

简单的python脚本。 输入和输出不匹配

[英]Simple python script. Input and output do not match

Short version: The number of lines in the input and output files are not equal. 简短版本:输入和输出文件中的行数不相等。 I don't understand why as I am not adding or subtracting any lines ie input file has 12327 lines but output only has 11903. 我不明白为什么不添加或减去任何行,即输入文件有12327行,而输出只有11903行。

I can't seem to find any fault with my code. 我的代码似乎找不到任何错误。 I was hoping someone with more experience could? 我希望有更多经验的人可以吗? Thank you. 谢谢。

Detailed version: I have a simple python script made up of 3 functions. 详细版本:我有一个由3个函数组成的简单python脚本。

Function 1 takes a list of numbers from a .txt file (infile), converts all of them into floats, and changes any negative numbers to 0. It then adds them to a list (orginal list). 函数1从.txt文件(infile)中获取数字列表,将它们全部转换为浮点数,并将所有负数更改为0。然后将它们添加到列表(原始列表)中。 Each element of this list is then printed to an output file. 然后,此列表的每个元素都会打印到输出文件中。 It also tells you the number of lines in the input file. 它还告诉您输入文件中的行数。

Function 2 essentially checks the number of lines in the output file. 函数2本质上检查输出文件中的行数。

Function 3 is just runs functions 1 and 2. 功能3只是运行功能1和2。

The input file has 12327 lines whilst the output file only has 11903. 输入文件有12327行,而输出文件只有11903行。

I made another input file to test the script. 我制作了另一个输入文件来测试脚本。 This .txt input file has 10 lines a few of which are negative numbers. 该.txt输入文件有10行,其中一些是负数。 However the output file is completely empty. 但是,输出文件完全为空。

I don't understand why the input and output number of lines are different? 我不明白为什么输入和输出的行数不同? I have included the code below. 我已包含以下代码。

I would appreciate any advice. 我将不胜感激任何建议。 Thank you. 谢谢。

def thelist():  #FUNCTION 1
    original_list = []
    for line in infile:
        newline = float(line)
        if newline < 0:
            newline = 0
        original_list.append(newline)
    print('The input file has', len(original_list), 'lines.')
    for element in original_list:
        print(element, file = outfile)

def outfilelinenumber():    #FUNCTION 2
    outfile = open('outfile.txt')
    improved_list = []
    for line in outfile:
        improved_list.append(line)
    if not improved_list:
        print('Output file is empty.')
    else:
        print('The output file has', len(improved_list), 'lines.')

def main(): #FUNCTION 3
    thelist()
    outfilelinenumber()
    print('\n', '*** Finished ***')


infile = open('infile.txt')
outfile = open('outfile.txt', 'w')

main()

One problem is that you never close your output file, so nothing is properly sent to it. 一个问题是您永远不会关闭输出文件,因此没有正确发送任何文件。 You need to take care of your files: if you open it, close it properly. 您需要照顾好文件:如果打开它,请正确关闭它。 You also do not close your input file, though you do not see the problems that can result from that. 您也不会关闭输入文件,尽管您看不到由此引起的问题。

Replace your function with 替换为

def thelist():  #FUNCTION 1
    original_list = []
    for line in infile:
        newline = float(line)
        if newline < 0:
            newline = 0
        original_list.append(newline)
    print('The input file has', len(original_list), 'lines.')
    for element in original_list:
        print(element, file = outfile)
    infile.close()
    outfile.close()

Note the two close functions at the end. 请注意最后两个关闭函数。 This takes care of your problem in my tests, though you do have other problems with your file handling that could cause problems. 尽管您的文件处理确实存在其他问题,但可能会引起问题,因此可以解决我的测试中的问题。 I suggest you look at the tutorial on file handling to learn better file-handling. 我建议您阅读有关文件处理教程,以学习更好的文件处理。

I can see some lines are not fully pushed to outfile.txt by the time the second method reads outfile, and finds it empty. 我可以看到,当第二种方法读取outfile并将其发现为空时,某些行并未完全推送到outfile.txt。 This is because of the write operation not getting completed. 这是因为写入操作未完成。 As suggested in the previous answer by Rory, it is suggested to close the files after use for proper file handling. 正如Rory先前的回答所建议的那样,建议在使用后关闭文件以进行正确的文件处理。 Even using flush() to complete any pending write operations will help here. 即使使用flush()完成所有挂起的写操作也将有所帮助。

Please use below main() for proper results. 请在main()下面使用以获得正确的结果。

 def main(): #FUNCTION 3
    thelist()
    infile.flush()
    outfile.flush()
    outfilelinenumber()
    infile.flush()
    outfile.flush()
    print('\n', '*** Finished ***')

Using this the output was: 使用此输出是:

 The input file has 13 lines.
 The output file has 13 lines.

  *** Finished ***

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

相关问题 我如何正确使用函数调用。 我要做2批python脚本。 一个在输入中,另一个在输出中 - How do i use function call properly. I am gonna do 2 batch python script. The one is in input the other one is output glob文件,用作python脚本中python脚本的输入。 - glob files to use as input for a python script from a python script. Python:在后台运行脚本并执行输入/输出 - Python: run script in background and do input/output 使用Python ssh然后运行bash脚本。 我需要在屏幕上显示正在运行的bash脚本的输出 - Using Python to ssh then run bash script. The output of the running bash script i need to display on the screen 简单python脚本中不需要的输出 - Unwanted output in simple python script 双击一个文件以运行python脚本。 如何获得该文件作为输入? - double click a file to run python script. how to get that file as an input? Excel中的Python脚本输入和输出 - Python script input and output in excel 用python脚本激活root用户。 (OSX) - Activating the root user with a python script. (OSX) 基本的Python加法脚本问题。 - Problems with a basic Python addition script. AWS Lambda Python 脚本。 如何返回对象并保持 scipt 在后台运行? - AWS Lambda Python script. How do I return an object and keep the scipt running in the background?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM