用大写字母写吗?

发布于 2024-12-25 16:39:00 字数 379 浏览 3 评论 0原文

我有下一个代码:

    bestand = open("6_frame.txt", "r")
    seq = bestand.readlines()
    #to make a list
    for line in seq:
        alle = line
        while True: 
            if alle.isupper():
                break
            else:
                print ("try again ")

使用此代码,我想确保有人在文件中写入序列,用大写字母写入此序列,并希望排除其他错误:但他想做我想做的事。

有人可以帮助我吗?

i have the next code:

    bestand = open("6_frame.txt", "r")
    seq = bestand.readlines()
    #to make a list
    for line in seq:
        alle = line
        while True: 
            if alle.isupper():
                break
            else:
                print ("try again ")

with this code i want to make sure that someone , who write a sequence in the file, write this sequence in capital letters, and want to except the other errors: but he want do what i want.

can somebody help me ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事↓在人 2025-01-01 16:39:00

我认为你是说你想确保整个文件都是大写的。如果这就是您正在寻找的内容,那么这将解决问题:

if all(x.isupper() for x in open("6_frame.txt", "r")):
    print("entire file is upper-case.")
else:
    print("try again!")

这将一次测试文件中的所有大写字符。如果它找到不是的行,它将返回 false,否则如果所有行都是大写的,则返回 true(并打印“整个文件都是大写的”)。

更新(文件监视版)

看起来您想继续检查文件直到它全部大写。这是一种相当低效的方法(您可以添加 modtime 检查,或使用 inotify 使其更好):

from time import sleep

while True:
    lines = open("6_frame.txt", "r")
    if all((x.isupper() or x.isspace()) for x in lines):
        print("entire file is upper-case.")
        break  # We're done watching file, exit loop
    else:
        print("try again!")

    sleep(1)   # Wait for user to correct file

此外,如果当您的脚本检查文件时该人正在保存中,您可能会遇到异常(我不确定)再次,因此您可能需要在 all 行周围添加一些异常捕获。不管怎样...希望这有帮助!

I think you are saying you want to ensure the entire file is in upper-case. If that's what you are looking for, this will do the trick:

if all(x.isupper() for x in open("6_frame.txt", "r")):
    print("entire file is upper-case.")
else:
    print("try again!")

This will test the file, line-at-a-time, for all upper-case characters. If it finds a line that is not, it will return false, otherwise if all lines are upper-case, return true (and print "entire file is upper-case").

Update (File watching edition)

It looks like you want to keep checking the file until it's all uppercase. Here's a fairly ineffecient way to do it (you could add modtime checks, or use inotify to make it better):

from time import sleep

while True:
    lines = open("6_frame.txt", "r")
    if all((x.isupper() or x.isspace()) for x in lines):
        print("entire file is upper-case.")
        break  # We're done watching file, exit loop
    else:
        print("try again!")

    sleep(1)   # Wait for user to correct file

Also, you may get exceptions (I'm not sure) if the person is mid-save when your script checks the file again, so you may need to add some exception catching around the all line. Either way... Hope this helps!

攀登最高峰 2025-01-01 16:39:00

我的 abc.txt 内容是 aBC,因此并非全部大写:

fd = open('abc.txt','r')
seq = fd.readlines()
for line in seq:
       if line.isupper():
                  print('all capital')
       else:
                print('try again')

我的输出 = 重试

因此,如果我的 abc.txt, code> 内容是 ABC 我的输出是 全部大写

My abc.txt content is aBC, so not all uppercase:

fd = open('abc.txt','r')
seq = fd.readlines()
for line in seq:
       if line.isupper():
                  print('all capital')
       else:
                print('try again')

therefore my output = try again

if my abc.txt content is ABC my output is all capital

情绪失控 2025-01-01 16:39:00

判断文件中的所有大小写字符是否都是大写,如果不是则重试:

import time
from hashlib import md5

hprev = None
while True:
    with open("6_frame.txt") as f:
         text = f.read()
         if text.isupper():
            print('all capital')
            break
         else:
            h = md5(text).hexdigest()
            if h != hprev: # print message if the file changed
               print('try again')
            hprev = h
            time.sleep(1) # wait for the file to change

Determine whether all cased characters in the file are uppercase, retry if not:

import time
from hashlib import md5

hprev = None
while True:
    with open("6_frame.txt") as f:
         text = f.read()
         if text.isupper():
            print('all capital')
            break
         else:
            h = md5(text).hexdigest()
            if h != hprev: # print message if the file changed
               print('try again')
            hprev = h
            time.sleep(1) # wait for the file to change
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文