[英]Reading multiple lines from an external text file in Python
當我使用代碼時,該程序運行正常
for character in infile.readline():
問題是readline僅讀取一行文本。 當我在readline命令中添加“ s”時
for character in infile.readlines():
我最終得到0的輸出。
os.chdir(r'M:\Project\Count')
def main():
infile = open("module3.txt","r")
uppercasecount = 0
lowercasecount = 0
digitcount = 0
spacecount = 0
for character in infile.readlines():
if character.isupper() == True:
uppercasecount += 1
if character.islower() == True:
lowercasecount += 1
if character.isdigit() == True:
digitcount += 1
if character.isspace() == True:
spacecount += 1
print ("Total count is %d Upper case, %d Lower case, %d Digit(s) and %d spaces." %(uppercasecount, lowercasecount, digitcount, spacecount))
main()
另外,如果有人可以給我建議,我可以將該目錄設置為默認位置,以便我可以在其他計算機上使用該目錄並使用它。
您可以使用兩種形式的iter
讀取任意數量的字節,然后使用itertools.chain
將其視為一個長輸入。 您可以使用str
方法作為collections.Counter
鍵,而不是跟蹤幾個變量,例如:
from collections import Counter
from itertools import chain
counts = Counter()
with open('yourfile') as fin:
chars = chain.from_iterable(iter(lambda: fin.read(4096), ''))
for ch in chars:
for fn in (str.isupper, str.islower, str.isdigit, str.isspace):
counts[fn] += fn(ch)
#Counter({<method 'islower' of 'str' objects>: 39, <method 'isspace' of 'str' objects>: 10, <method 'isdigit' of 'str' objects>: 0, <method 'isupper' of 'str' objects>: 0})
然后counts[str.lower]
會給你39
。
如果您只想檢查文件中包含的角色的類型,則我不會使用readlines而是常規的read 。
STEP_BYTES = 1024
def main():
infile = open("module3.txt","r")
uppercasecount = 0
lowercasecount = 0
digitcount = 0
spacecount = 0
data = infile.read(STEP_BYTES)
while data:
for character in data:
if character.isupper() == True:
uppercasecount += 1
if character.islower() == True:
lowercasecount += 1
if character.isdigit() == True:
digitcount += 1
if character.isspace() == True:
spacecount += 1
data = infile.read(STEP_BYTES)
print ("Total count is %d Upper case, %d Lower case, %d Digit(s) and %d spaces." %(uppercasecount, lowercasecount, digitcount, spacecount))
main()
如果您確實需要使用readlines
,請記住,該方法將讀取文件的所有行並將它們以行列表的形式放入內存中(對於大文件來說效果不佳)。
例如,假設您的module3.txt
文件包含:
this Is a TEST
and this is another line
使用readlines()
將返回:
['this Is a TEST\n', 'and this is another line']
考慮到這一點,您可以使用double for
循環遍歷文件內容:
def main():
infile = open("module3.txt","r")
uppercasecount = 0
lowercasecount = 0
digitcount = 0
spacecount = 0
lines = infile.readlines()
for line in lines:
for character in line:
if character.isupper() == True:
uppercasecount += 1
if character.islower() == True:
lowercasecount += 1
if character.isdigit() == True:
digitcount += 1
if character.isspace() == True:
spacecount += 1
print ("Total count is %d Upper case, %d Lower case, %d Digit(s) and %d spaces." %(uppercasecount, lowercasecount, digitcount, spacecount))
main()
至於目錄,如果您的代碼和文本文件( module3.txt
)將放在同一目錄中,則無需執行chdir
。 默認情況下,腳本的工作目錄是腳本所在的目錄。
假設您將其運送到以下目錄中:
|-> Count
|-> script.py
|-> module3.txt
您可以使用相對路徑從script.py
打開module3.txt
: open("module3.txt", "r")
行將查找一個名為module3.txt
的文件,其中包含腳本運行的目錄(意思是Count\\
)。 您不需要調用os.chdir
。 如果你仍然想確保,你可以chdir
到腳本所在(看看到目錄此 ):
知道這一點后,將硬編碼的chdir
行(文件頂部的os.chdir(r'M:\\Project\\Count')
)更改為:
print "Changing current directory to %s" % os.path.dirname(os.path.realpath(__file__))
os.chdir(os.path.dirname(os.path.realpath(__file__)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.