I'm trying to write Python code to detect the first non-whitespace character in each line in a file and check if it is a "}". For example, if the contents of my file are...
a
fox {
}
{ jumped
} up
... I'd want to detect the "}" in the third line despite it having two whitespaces, and in the fifth line.
I've tried doing something like this but I'm stuck:
full_file = open ("filename", "r")
each_line = full_file.readlines()
for item in each_line
if item[0].find('}') != -1:
# do something, such as print (item)
full_file.close()
Help much appreciated!
You may try calling strip()
on each line, then checking just the first character:
full_file = open ("filename", "r")
each_line = full_file.readlines()
for item in each_line
if item.strip() and item.strip()[0] == '}':
print(item)
full_file.close()
You could use this function to get the first non whitespace character.
This only reads one line at a time, so it could save you some trouble if you're dealing with a very large file.
def get_first_non_whitespace_char(file_name):
with open(file_name, 'r') as fileobj:
for line in fileobj:
for character in line:
if character != " ":
return character
return None
Trying out with a file like this:
sample.txt
}hello
Using the function
file_name = "sample.txt"
first_nsp_char = get_first_non_whitespace_char(file_name)
if first_nsp_char != None:
print("First Non space character in file:", first_nsp_char)
print("Character is '}' :", first_nsp_char == '}')
else:
print("No non-whitespace characters found in file:", file_name)
Output:
First Non space character in file: }
Character is '}' : True
my version on slight different input (sample.txt has two empty lines at the end, '\n'):
a
fox {
}
{ jumped
} up
is:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 10 11:28:29 2021
@author: Pietro
https://stackoverflow.com/questions/67733277/how-can-i-detect-the-first-non-whitespace-character-in-each-line-of-a-file-opene/67733429#67733429
"""
def get_first_non_whitespace_char(file_name):
listz ={}
index = 0
with open(file_name, 'r') as fileobj:
for line in fileobj:
index += 1
#print('line: ',line)
for character in line:
if character != " " and character != '\n':
listz[index] = character
# index += 1
break
else:
pass
return listz
file_name = "sample.txt"
first_nsp_char = get_first_non_whitespace_char(file_name)
for i in first_nsp_char:
print('line # ',i,' first character is : ',first_nsp_char[i])
output:
line # 1 first character is : a
line # 2 first character is : f
line # 3 first character is : }
line # 4 first character is : {
line # 7 first character is : }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.