[英]how to find the index of the first non-whitespace character in a string in python?
[英]How can I detect the first non-whitespace character in each line of a file opened in Python?
我正在尝试编写 Python 代码来检测文件中每一行中的第一个非空白字符并检查它是否是“}”。 例如,如果我的文件内容是...
a
fox {
}
{ jumped
} up
...我想检测第三行中的“}”,尽管它有两个空格,并且在第五行。
我试过做这样的事情,但我被困住了:
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()
非常感谢帮助!
您可以尝试在每一行调用strip()
,然后只检查第一个字符:
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()
您可以使用此 function 来获取第一个非空白字符。
这一次只读取一行,因此如果您正在处理一个非常大的文件,它可以为您节省一些麻烦。
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
尝试使用这样的文件:
sample.txt
}hello
使用 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
我的版本略有不同的输入(sample.txt 末尾有两个空行,'\n'):
a
fox {
}
{ jumped
} up
是:
#!/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 : }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.