简体   繁体   English

从txt文件中读取数据

[英]Reading data from txt file

def main():
    file_output=open(r'C:\Users\P\Desktop\randominput.txt','r')

    for num in file_output:
        number=file_output.read()
        print(number)
main()

output:输出:

48
85
58
16
11
82

Actual data in the txt file is txt文件中的实际数据是

53
48
85
58
16
11
82

Why is my python program not reading the first number??为什么我的python程序没有读取第一个数字?

Use a simple print to trace what's going on:使用简单的打印来跟踪正在发生的事情:

for num in file_output:
    print("num=", num)
    number=file_output.read()
    print(number)

Output:输出:

num= 53

48
85
58
16
11
82

Note the effect: num is the first input number, but it appears only once.注意效果: num是第一个输入的数字,但只出现一次。 You go through the loop only once.你只经过一次循环。

Your for loop grabs the first number out of the file;你的for循环从文件中抓取第一个数字; the read within the loop grabs the remainder of the file as one input.循环中的读取获取文件的其余部分作为一个输入。

I suggest that you look up that actual function of each command you're using;我建议您查找您正在使用的每个命令的实际功能; you're not quite clear on the operation yet.你对操作还不是清楚。


REPAIR修理

def main():
    with open("so.txt") as file_output:

        for num in file_output:
            print("num=", num)
main()

Output:输出:

num= 53

num= 48

num= 85

num= 58

num= 16

num= 11

num= 82

Note that you're still reading these as strings, complete with newline character.请注意,您仍将这些作为字符串阅读,并带有换行符。 Continuing from here is left as an exercise for the reader.从这里继续作为练习留给读者。 :-) :-)

The first line of the file is consumed during the first iteration of the for loop.在 for 循环的第一次迭代期间使用文件的第一行。 The read() then reads the file from the current position in the file, which is the beginning of the second line, to the end of the file. read()然后从文件中的当前位置(即第二行的开头read()读取文件,直到文件的结尾。

So the print() is printing the contents of the file in one go, having skipped over the first line.所以print()打印文件的内容,跳过了第一行。

To fix your code:要修复您的代码:

with open(r'C:\Users\P\Desktop\randominput.txt', 'r') as file_input:
    for number in file_input:
        print(number.rstrip())

The rstrip() is there to remove the new line character at the end of each line. rstrip()用于删除每行末尾的换行符。 Also the file is opened in a context manager using with .该文件也在上下文管理器中使用with打开。 This will ensure that the file will be closed in all circumstances.这将确保文件在所有情况下都将关闭。

is it a typo that in the "open(r'C:\\Users\\P\\Desktop\\randominput.txt','r')" there is an r before 'C\\Users\\P...'?在“open(r'C:\\Users\\P\\Desktop\\randominput.txt','r')”中,'C\\Users\\P...' 之前有一个 r 是错字吗? that shouldn't be there.那不应该在那里。

I believe you are trying to print every number from a text file.我相信您正在尝试打印文本文件中的每个数字。 here is a way you could do this:这是您可以执行此操作的一种方法:

f = open("C:/Users/u/file.txt")
file_contents = f.read()
numbers = str(file_contents).split()
for x in range (0, len(numbers) - 1):
    print (str(numbers[x]))

Your program is reading the first number.您的程序正在读取第一个数字。 When you begin iterating over the file object, for num in file_output: the first line of the file is read, so num is the first number and the file reader is now looking at the second, waiting for the next iteration.当您开始迭代文件对象时, for num in file_output:读取文件的第一行,因此num是第一个数字,文件读取器现在正在查看第二个数字,等待下一次迭代。

You then call .read() which will read to the end of the file.然后调用.read() ,它将读取到文件的末尾。 This gives you all the rest of the numbers in number and moves the reader to the end of the file, terminating the iteration.这给你的数字其余全部在number和移动阅读器在文件的结尾,终止迭代。 You want either:你想要:

for num in file_output:
    print(num)

or或者

numbers = file_output.read()
for n in numbers:
    print(n)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM