简体   繁体   English

我不明白为什么我不能让 python 中的 readline function 在这个程序中工作。 我究竟做错了什么?

[英]I don't understand why i can't get readline function in python to work in this program. What am I doing wrong?

In this program I want to save individual lines to a variable but when i try to print the variable it just returns a space instead of whats on the line in the file.在这个程序中,我想将单独的行保存到一个变量中,但是当我尝试打印该变量时,它只返回一个空格,而不是文件中行上的内容。 sorry i'm quite new to programming抱歉,我对编程很陌生

file=open('emails.txt','w+')
while True:

    email=input('pls input your email adress: ')
    file.write(email)
    file.write('\n')
    more=input('would you like more emails to be processed? ')
    if more == 'yes' or more == 'ye' or more == 'y' or more == 'yep' or more == 'Y':
        continue

    elif more == 'no' or more == 'nah' or more == 'n' or more == 'N' or more == 'nope':
        file.close()
        print('this is the list of emails so far')
        file=open('emails.txt','r')
        print(file.read()) #this reads the whole file and it works
        email_1=file.readline(1) #this is meant to save the 1st line to a variable but doesn't work!!!
        print(email_1) #this is meant to print it but just returns a space
        file.close()
        print('end of program')

First, you should use with to work with files.首先,您应该使用with来处理文件。

Second, you open the file for printing and read all of its content: print(file.read())其次,您打开文件进行打印并读取其所有内容: print(file.read())

after this line, the cursor is in the end of the file, so next time you try to read something from the file, you get empty string.在这一行之后,cursor 位于文件末尾,因此下次尝试从文件中读取内容时,会得到空字符串。

to fix it, you have few alternatives.要修复它,您几乎没有其他选择。

First option:第一个选项:

add file.seek(0, 0) to move the cursor back to the start of the file so when you do file.readline , you will realy read the file line.添加file.seek(0, 0)将 cursor 移回文件的开头,因此当您执行file.readline时,您将真正读取文件行。

moreover, file.readline(1) should be changed to file.readline()此外, file.readline(1)应改为file.readline()

Second option:第二种选择:

just read all the file content into a list, print it and then print the first entry in the list(the first line in the file...)只需将所有文件内容读入列表,打印它然后打印列表中的第一个条目(文件中的第一行......)

file = open('emails.txt', 'r')
content = file.readlines()
print(*content, sep='')
email_1 = content[0] 
print(email_1)  

As mentioned in the first comment above, the file.read() call is moving your file pointer to the end of the file, so there is no data left for the readline() to read.正如上面第一条评论中提到的,file.read() 调用将文件指针移动到文件末尾,因此没有数据可供 readline() 读取。

But also, you are calling readline(1) which will read one byte, not one line.而且,您正在调用 readline(1) 它将读取一个字节,而不是一行。

Well I would try to use with好吧,我会尝试with

So try to implement it like this:所以尝试像这样实现它:

> with open('emails.txt','w+') as output_file:  
   while True:
    # and then rest of your code

暂无
暂无

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

相关问题 Spotify Best Before计划。 我究竟做错了什么? - Spotify Best Before program. What am I doing wrong? 我无法让QueryAABB在PyBox2D中工作。 我究竟做错了什么? - I can't get QueryAABB to work in PyBox2D. What am I doing wrong? Python错误。 我不明白我在做什么错 - Python error. I don't understand what I'm doing wrong 我的函数中的打印语句出现语法错误。 我不知道我做错了什么 - I get syntax error on my print statement in my function. I don't know what I am doing wrong 我无法理解我在 Kaggle 比赛中的训练和测试数据做错了什么 - I can't understand what I am doing wrong with my training and my test data for a competition on Kaggle 无法让 Spyder 使用 Git:我做错了什么? - Can't get Spyder to work with Git: what am I doing wrong? 我不明白为什么这段代码不起作用! 有人可以告诉我我做错了吗? - I don't see why this code is not working! can someone please tell me what i am doing wrong? show() function 给了我 15 我明白但是为什么 print(x) function 返回 13 我不明白我是 Z23EEEB4347BDD26BFC6B7EE9A3B755DDs 的新手 - the show() function gives me 15 & I understand but why does the print(x) function return 13 I don't get it I am new to python can you pls help me 我究竟做错了什么? CSS 文件在 Django 项目中不起作用 - what am i doing wrong? CSS files don't work in Django project 我不明白这段代码有什么问题 - I don't understand what is wrong with this code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM