【问题标题】:python Filtering the file contentspython过滤文件内容
【发布时间】:2021-03-17 06:44:07
【问题描述】:

我是 python 新手,我正在尝试执行以下任务,但我的输出与预期的不同。任何人都可以帮我解决这里的问题吗?感谢您的帮助!

作业:

在第三个程序中,我们来看看文件内容的分类。在与源代码相同的目录中是一个文件“strings.txt”,其中包含多行随机字符串。这些行可以分为两组:只有字母(a-z、A-Z)和数字(0-9)的行,以及也有随机特殊字符(?、&、@、$ ...)的行。

创建一个程序,从文件中读取所有行并测试这些行。如果该行只有字母和/或数字,程序将打印“[line] was ok.”。如果该行有特殊字符,程序应该打印“[line] was invalid.”。当程序运行时,它会打印出如下内容:

5345m345ö34l was ok.
no2no123non4 was ok.
noq234n5ioqw#% was invalid.
%#""SGMSGSER was invalid.
doghdp5234 was ok.
sg,dermoepm was invalid.
43453-frgsd was invalid.
hsth())) was invalid.
bmepm35wae was ok.
vmopaem2234+0+ was invalid.
gsdm12313 was ok.
bbrbwb55be3"?"#? was invalid.
"?"#%#"!%#"&"?%%"?#?#"?" was invalid.
retrte#%#?% was invalid.
abcdefghijklmnopqrstuvxy was ok.

建议一次读取一行,使用 isalmun() 字符串测试对其进行测试,然后从那里继续。还要记住,字符串也可能以换行符 (\n) 结尾,这是允许的,但如果没有被切掉,则 .isalnum() 测试失败。 示例输出

5345m34534l was invalid.
no2no123non4 was ok.
noq234n5ioqw#% was invalid.
%#""SGMSGSER was invalid.
doghdp5234 was ok.
sg,dermoepm was invalid.
43453-frgsd was invalid.
hsth())) was invalid.
bmepm35wae was ok.
vmopaem2234+0+ was invalid.
gsdm12313 was ok.
gswrgsrdgrsgsig45 was ok.
)/(/)(#=%#)%/ was invalid.
++-+-+--+--+-+>-<+-<<_<-+>>++ was invalid.

我的代码是

handle = open("strings.txt","r")
content = handle.read()
content.isalnum()
for i in content:
    if content.isalnum()==True:
        print(content,"was ok")
    else:
        print(content,"was invalid")

handle.close()

我的输出是

5345m34534l
no2no123non4
noq234n5ioqw#%
%#""SGMSGSER
doghdp5234
sg,dermoepm
43453-frgsd
hsth()))
bmepm35wae
vmopaem2234+0+
gsdm12313
gswrgsrdgrsgsig45
)/(/)(#=%#)%/
++-+-+--+--+-+>-<+-<<_<-+>>++. was invalid
5345m34534l
no2no123non4
noq234n5ioqw#%
%#""SGMSGSER
doghdp5234
sg,dermoepm
43453-frgsd
hsth()))
bmepm35wae
vmopaem2234+0+
gsdm12313
gswrgsrdgrsgsig45
)/(/)(#=%#)%/
++-+-+--+--+-+>-<+-<<_<-+>>++. was invalid
5345m34534l
no2no123non4
noq234n5ioqw#%
%#""SGMSGSER
doghdp5234
sg,dermoepm
43453-frgsd
hsth()))
bmepm35wae
vmopaem2234+0+
gsdm12313
gswrgsrdgrsgsig45
)/(/)(#=%#)%/
++-+-+--+--+-+>-<+-<<_<-+>>++. was invalid

# etc ad nauseum...

我做错了什么?

【问题讨论】:

    标签: python


    【解决方案1】:
    handle = open("strings.txt","r")
    content = handle.read()  # <= here you read in the entire file
    content.isalnum()
    for i in content:      # <= here you iterate over each **character** of the file
        if content.isalnum()==True:
            print(content,"was ok")
                   # ^ here you print the entire input file each time
        else:
            print(content,"was invalid")
                   # ^ (ditto) which is why you have so much output
    handle.close()
    

    不如试试

    with open("strings.txt") as inf:
        for line in inf:
            line = line.rstrip()
            if line.isalnum():
                print("{} was ok".format(line))
            else:
                print("{} was invalid".format(line))
    

    【讨论】:

    • 您好,除了一个错误,它看起来正在工作。在“无效”之前的行尾有一个额外的“。”。像这样:+-+-+--+--+-+>->++。无效。但应该是这样的:+-+-+--+--+-+>->++ 无效。点(。)从哪里来?
    • @user3358884:检查你的输入文件,我几乎可以保证你的时间段在那里。
    • 嗨,输入文件不是我的,它是系统生成的输入。我在 viope.com 上做作业,这是一个自学工具,您只需编写代码,系统会通过提供自己的输入文件来检查您的代码。
    【解决方案2】:

    我认为输出是错误的,因为您将整个文件读入一个字符串并且 循环遍历文件的所有字符。您的“内容”变量是一个字符串,因此代码不会逐行检查。它检查整个文件并打印出整个文件无效。

    我的回答:

    file = open("strings.txt","r") content = file.readlines() for i in content: if i.rstrip().isalnum(): print(i+" was ok.") else: print(i+" was invalid.") file.close()

    【讨论】:

      【解决方案3】:

      这是我的作业代码,它提供了所需的输出:

      filename="strings.txt"
      handle = open("strings.txt","r") #read file
      text = handle.readlines() # read lines by lines
      for i in text:
          if i.rstrip().isalnum():
              print(i.rstrip()," was ok.")
          else:
              print(i.rstrip()," was invalid.")
      handle.close()
      

      【讨论】:

        【解决方案4】:
        handle = open("strings.txt","r")
        
        line = ""
        while True:
            line = handle.readline()
            if line == "":
                break
            line = line[:-1]
            if line.isalnum() == False:
                print(line,"was invalid.")
            else:
                print(line,"was ok.")
        
        handle.close()
        

        【讨论】:

          【解决方案5】:
          file = open("strings.txt", "r")
              content = file.readlines()
              file.close()
              for line in content:
                  line = line.strip()
                  if line.isalnum():
                      print(line + " was ok.")
                  else:
                      print(line + " was invalid.")
          

          【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-13
          • 2013-09-16
          • 1970-01-01
          • 2019-09-24
          • 2012-06-14
          相关资源
          最近更新 更多