python Filtering the file contents

python find string in file and print line
python read file with filter
python read file and split lines
python read file line by line example
how to read multiline string as data from file in python
python read file line by line into list
python read next line after match
different ways to read a file in python

I am new to python and I am trying to do the following assignment but my output is not the same as the one it is supposed to be. Can any one help me what the problem is here? I appreciate your helps!

Assignment:

In the third program, we take a look into the classification of file contents. In the same directory with the source code is a file "strings.txt", which has random strings in several lines. The lines can be divided into two groups: those which only have letters (a-z, A-Z) and numbers (0-9), and those which also have random special characters (?,&,@, $ ...).

Create a program which reads all of the lines from the file and tests the lines. If the line has only letters and/or numbers, the program prints "[line] was ok.". If the line has special characters, the program should print "[line] was invalid.". When the program works, it prints out something like this:

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.

It is advisable to read the lines one at a time, test them with the isalmun() string test and go on from there. Also remember that the strings may also end in a line break (\n), which is allowed, but fails the .isalnum() test if not sliced away. Example output

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.

my code is

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()

My output is

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...

What am I doing wrong?

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()

instead, try

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))

filtering some sequences from txt file in python, Hi, I have a GFF file containing MCF-7 cell transcript data. There is also a fastq file if that Content. Search � Users� It may be hard for novice python programmers to really get used to using list comprehensions for filtering, and it can have some memory overhead for very large data sets, but for listing a directory and other simple string filtering tasks, list comprehensions lead to more clean documentable code.

I think the output was wrong because you read entire file into one string and loop over all character of the file. Your "content" variable is a string, so the code don't check line by line. It checks the entire file and prints that entire file was invalid.

My answer:

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()

How to Extract Specific Portions of a Text File Using Python, How the string data in a list can be filtered in Python is shown in this article by using different print("The content of the second list after filter:", filter_data). To modify huge CSV or XLSX files, such as exports from your Salesforce “Task” and “Contact” tables, consider writing code with a language like Python. Why Excel modifies large files slowly If you work with huge spreadsheets, you’ve probably frozen Excel by trying to filter a file and delete certain rows.

Here is my code for that homework and it gives the desired output:

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()

How to filter a list of strings in Python – Linux Hint, txt in same directory as our python script. Let's see how to read it's contents line by line. Solution for Small Files : Get list of all lines in file using� Filtering rows of a DataFrame is an almost mandatory task for Data Analysis with Python. Given a Data Frame, we may not be interested in the entire dataset but only in specific rows. Related course: Data Analysis with Python Pandas. Filter using query A data frames columns can be queried with a boolean expression.

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.")

5 Different ways to read a file line by line in Python – thispointer.com, Constructs a file dialog with the given parent and caption that initially displays the contents of the specified directory . The contents of the directory are filtered� In versions of Python prior to Python 3, os.listdir () is the method to use to get a directory listing: >>>. >>> import os >>> entries = os.listdir('my_directory/') os.listdir () returns a Python list containing the names of the files and subdirectories in the directory given by the path argument: >>>.

QFileDialog — Qt for Python, Filter current file using an external command (these examples use sort , but Vim has Following is a Python program to sort the words on each line of standard� In the above example, we used two steps, 1) create boolean variable satisfying the filtering condition 2) use boolean variable to filter rows. However, we don’t really have to create a new boolean variable and save it to do the filtering. Instead, we can directly give the boolean expression to subset the dataframe by column value as follows.

Use filter commands to process text | Vim Tips Wiki, This iterates over the lines of all files listed in sys.argv[1:] , defaulting to sys.stdin Optional in-place filtering: if the keyword argument inplace=True is passed to� A Python program can read a text file using the built-in open () function. For example, the Python 3 program below opens lorem.txt for reading in text mode, reads the contents into a string variable named contents, closes the file, and prints the data.

fileinput — Iterate over lines from multiple input streams — Python , Filter Out Directories. Now that we have all the contents of the directory, it's time to filter out the directories—leaving only the files. from os� If we have a small file, then we can call readlines() on the file handler, it reads the whole file content to memory, then splits it into seperate lines and returns a list of all lines in the file. All the lines in list except the last one, will contain new line character in end.

Comments
  • Hi it looks it is working except one error. at the end of the line before "was invalid" there is an extra ".". like this: ++-+-+--+--+-+>-<+-<<_<-+>>++. was invalid. But it should be like this:++-+-+--+--+-+>-<+-<<_<-+>>++ was invalid. where is the dot(.) coming from?
  • @user3358884: check your input file, I pretty much guarantee you the period is there.
  • Hi, the input file is not mine it is system generated input. I am doing the assignment on viope.com, a self study tool, and you just do the code and the system checks your code by giving its own input file.
  • Welcome to stackoverflow. Your answer would be improved by explaining how it addresses the question. See stackoverflow.com/help/how-to-answer