[英]Reading data from a txt file from string line
我需要編寫一個程序來讀取存儲在txt文件中的數據。 例如:
3147 R 1000
2168 R 6002
5984 B 2000
7086 B 8002
第一個數字是“帳號”,“ R”是住宅分類,最后一個數字是“使用的加侖”。 我需要產生一個程序來打印此:
Account Number: 3147 Water charge: $5.0
我需要讓代碼讀取文本中的4行。 居民客戶每6000加侖支付$ 005每加侖。 商業客戶為每8000加侖每加侖$ .006。如果更高,則$ .008我需要顯示每4行的乘積。 下面的代碼是我嘗試的。 我可能離我遠去。
我試過下面的代碼:
def main():
input_file = open('C:\Python Projects\Project
1\water.txt', 'r')
empty_str = ''
line = input_file.readline()
while line != empty_str:
account_number = int(line[0:4])
gallons = float(line[7:11])
business_type = line[5]
while business_type == 'b' or 'B':
if gallons > 8000:
water_charge = gallons * .008
else:
water_charge = gallons * .006
while business_type == 'r' or 'R':
if gallons > 6000:
water_charge = gallons * .007
else:
water_charge = gallons * .005
print("Account number:", account_number, "Water
charge:$", water_charge)
line = input_file.readline()
input_file.close()
main()
它只是運行而無需打印任何內容
兩件事情。 什么都不出現的原因是因為您陷入了檢查業務類型的while循環的無限循環中。 將其更改為if語句即可解決。
此外,當使用and或or或其他運算符時,必須再次指定要比較的變量。
您的while語句讀取文件的行應如下所示:
while line != empty_str:
account_number = int(line[0:4])
gallons = float(line[7:11])
business_type = line[5]
if business_type == 'b' or business_type =='B':
if gallons > 8000:
water_charge = gallons * .008
else:
water_charge = gallons * .006
if business_type == 'r' or business_type =='R':
if gallons > 6000:
water_charge = gallons * .007
else:
water_charge = gallons * .005
print("Account number:", account_number, "Water charge:$", water_charge)
line = input_file.readline()
輸出:
('Account number:', 3147, 'Water charge:$', 5.0)
主要的問題是while
循環應該只是if
語句:
if business_type.lower() == 'b':
if gallons > 8000:
water_charge = gallons * .008
else:
water_charge = gallons * .006
elif business_type.lower() == 'r':
if gallons > 6000:
water_charge = gallons * .007
else:
water_charge = gallons * .005
else:
# So you don't get a NameError for no water_charge
water_charge = 0
你的理由while
環路從來沒有終止的原因有二:你永遠不讀了內環內的下一行,像一個人口稠密的字符串'b'
將評估像True
:
# Strings with content act like True
if 'b':
print(True)
# True
# empty strings act as False
if not '':
print(False)
# False
str.lower()
將小寫字符串,因此'R'.lower()
返回'r'
。 否則,沒有break
的條件while
,它會永遠旋轉。
其他一些建議:
1)打開文件時,使用with
無需顯式open
和close
文件:
with open('somefile.txt', 'r') as fh:
# Everything else goes under the with statement
2) fh.readline()
顯式調用fh.readline()
,因為您可以直接迭代打開的文件:
with open('somefile.txt', 'r') as fh:
for line in fh:
# line is the string returned by fh.readline()
當fh
為空或到達文件末尾時,這也將終止,當文件為空時,可能不會顯式獲得''
,並且不再需要while
循環
3)這通常是不好的做法,但也很難維護。 例如,如果帳號不完全是5個字符怎么辦? 一種更簡潔的方法是使用str.split(' ')
,它將在空格處分割:
with open('somefile.txt', 'r') as fh:
for line in fh:
# This will split on spaces returning a list
# ['3147', 'R', '1000'] which can be unpacked into
# the three variables like below
account_number, business_type, gallons = line.split(' ')
# rest of your code here
總共:
# use with for clean file handling
with open('somefile.txt', 'r') as fh:
# iterate over the file directly
for line in fh:
# split and unpack
account_number, business_type, gallons = line.split(' ')
# enforce types
gallons = float(gallons)
# .strip() will remove any hanging whitespace, just a precaution
if business_type.strip().lower() == 'b':
if gallons > 8000:
water_charge = gallons * .008
else:
water_charge = gallons * .006
elif business_type.strip().lower() == 'r':
if gallons > 6000:
water_charge = gallons * .007
else:
water_charge = gallons * .005
else:
water_charge = 0.0
print("Account number:", account_number, "Water charge:$", water_charge)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.