Python loops don't work with readlines()
It should calculate "---------------------- --" number of lines, but it doesn't work, also using print("test") doesn't show up in the console, it always returns 0. But for example the line print("hi") works. The program just doesn't see my loop and I don't know why. :(
def check_id(): with open('data.txt', 'r') as f: lines = f.readlines() ad = 0 print("hi") # this line works for i in lines: print("test") # this line doesn't work if i == "-------------------------": ad += 1 return str(ad)
If I need to send the complete code to solve the problem, please ask
I changed the mode "a" to "r" so that it reads the rows correctly, and it does, but I still can't check the array to get the number of rows. If you have any guesses or solutions, please write them down.
EDIT: Here is the complete code for my data.py and the text in the file data.txt
from datetime import date date = date.today() def write_note(line): with open('data.txt', 'a') as f: if line == "!quit": f.write('\n') f.write("-------------------------") f.write('\n') ad = check_id() f.write(ad) f.write('\n') f.write("________________________") f.write('\n') else: f.write(line) f.write("\n") def read_note(id): with open('data.txt', 'r') as f: pass def see_all(): with open('data.txt', 'r') as f: get_lines = f.readlines() for i in get_lines: print(i) return get_lines def del_note(ad): with open('data.txt', 'a') as f: pass def logs(): pass def check_id(): with open('data.txt', 'r') as f: ad = 0 for i in f: if i == "-------------------------": ad += 1 return str(ad)
Now it is txt file:
fugy hello hai bebra ------------------------- 0 ________________________ uha imna fsjfoe geso;rsevdn ------------------------- 0 # This one ________________________
I'm trying to make a notebook so I can write notes and read them. Delete func I will do it later. The idea is to make this zero bigger every time you add an annotation.
Correct Answer
I think the problem is with your data.txt
file (probably empty as you mentioned "test"
is not visible in the console, which means that the script is not run inside a for
loop, in other words: lines
the length of the iterator is zero).
I have written a working code, you can see the code and test file with the script output below.
Code:
def check_id(): with open('data.txt', 'r') as opened_file: ad = 0 print("hi") # this line works for i in opened_file: print("test") # this line doesn't work if i == "-------------------------": ad += 1 return str(ad) result = check_id() print(f"result: {result}")
data.txt
:
test_1 ------------------------- test_2 ------------------------- test_3 ------------------------- test_4
test:
> python3 test.py hi test test test test test test test result: 0
edit:
op shared the complete source code and used data.txt
which contains cr
lf
characters (Details about the character ). This means that these rows must be striped using the rstrip
method.
In this case, only the check_id
function is relevant, so I only share the modified function:
def check_id(): with open('data.txt', 'r') as f: ad = 0 for i in f: # The cr and lf characters should be removed from line. Please see the above reference for details. if i.rstrip() == "-------------------------": ad += 1 return str(ad) result = check_id() print(result). # Result is 4
The above is the detailed content of Python loops don't work with readlines(). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

Fastapi ...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

Using python in Linux terminal...

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...
