新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python兩種不同的文件流讀寫
1、使用try進行異常發(fā)現(xiàn),使用while檢測文件末尾進行讀取

為饒平等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計制作服務(wù),及饒平網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站制作、成都網(wǎng)站設(shè)計、饒平網(wǎng)站設(shè)計,以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
file_to_read = raw_input("Enter file name of tests (empty string to end program):")
try:
infile = open(file_to_read, 'r')
while file_to_read != " ":
file_to_write = raw_input("Enter output file name (.csv will be appended to it):")
file_to_write = file_to_write + ".csv"
outfile = open(file_to_write, "w")
readings = (infile.readline())
print readings
while readings != 0:
global count
readings = int(readings)
minimum = (infile.readline())
maximum = (infile.readline())2、使用for遍歷讀取的每一行,進行一次性的讀取和輸入
result = list()
with open('../test/parameter.txt') as f:
for line in f.readlines():
temp = list()
# 逐個遍歷對應(yīng)每一行元素,將之轉(zhuǎn)為對應(yīng)的數(shù)據(jù)
b = line.strip(",][").split(',')
if(len(b) >= 5):
b.pop()
for a in b:
a = a.replace('[','').replace(']','')
temp.append(float(a))
result.append(temp)
#print("中途打印的temp是",temp)
#print("加入到result中的結(jié)果是",result)以上就是python兩種不同的文件流讀寫,希望對大家有所幫助。更多Python學(xué)習(xí)指路:創(chuàng)新互聯(lián)python教程
本文教程操作環(huán)境:windows7系統(tǒng)、Python 3.9.1,DELL G3電腦。
網(wǎng)站欄目:創(chuàng)新互聯(lián)Python教程:python兩種不同的文件流讀寫
分享地址:http://fisionsoft.com.cn/article/ccseeid.html


咨詢
建站咨詢
