我需要幫助導入文件並將每行轉換爲列表。Python:導入文件並轉換爲列表
的文件的一個例子將如下所示:
p wfgh 1111 11111 111111
287 48 0
65626 -1818 0
4654 21512 02020 0
具有p開始的第一行是一個首標,其餘均爲條款。 每個子句行必須以一系列的至少兩個整數的和結束以零
感謝開始提前
我需要幫助導入文件並將每行轉換爲列表。Python:導入文件並轉換爲列表
的文件的一個例子將如下所示:
p wfgh 1111 11111 111111
287 48 0
65626 -1818 0
4654 21512 02020 0
具有p開始的第一行是一個首標,其餘均爲條款。 每個子句行必須以一系列的至少兩個整數的和結束以零
感謝開始提前
如果你想在一個平面列表中的所有值,代碼如下所示:
ls=[]
for line in open("input.txt", "r").readlines():
for value in line.split(' '):
ls.append(value)
如果你只是想在一個列表中的行,那麼你可以在readlines方法停止()。
以下行將創建一個列表,其中每個項目是一個列表。內部列表是分成「單詞」的一行。
li = [i.strip().split() for i in open("input.txt").readlines()]
我把段,您投遞到在C input.txt的文件代碼:\ TEMP,跑這條線。輸出是否與您想要的相似?
C:\temp>python
Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print([i.strip().split() for i in open("input.txt").readlines()])
[['p', 'wfgh', '1111', '11111', '111111'], ['287', '48', '0'], ['65626', '-1818', '0'], ['4654', '21512', '02020', '0']]
fh=open("file")
mylist=[]
header=fh.readline().rstrip()
if not header.startswith("p wncf") :
print "error"
header=header.split()
mylist.append(header)
if len(header) != 5:
print "error"
if False in map(str.isdigit, header[2:]):
print "Error"
for line in fh:
line=line.rstrip().split()
if False in map(str.isdigit, line[0:2]):
print "Error"
elif line[-1] != 0:
print "Error"
else:
mylist.append(line)
fh.close()
fileName=open("d:/foo.bar")
lines = [i for i in fileName.readlines()]
希望幫助:d
三件事:1)'list(x)'是將一個迭代器變成一個列表比'[item for x]'更簡單更簡潔的方式。 2)'readlines'已經返回一個列表;你不需要遍歷它就可以把它變成一個。 3)創建一個名爲'fileName'的變量可能不是一個好習慣,然後在其中放入一些不是文件名的東西。 – 2009-12-15 20:17:31
感謝羅伯特的筆記 – 2009-12-16 16:01:29
p = open('filename')
#List:
linelist = [line for line in p.readlines()]
"""
But I prefer creating a dictionary as I find them more useful at times. Example here is very trivial. You can use the list index as a line number also.
"""
#Dictionary:
linedict = dict([(no, line) for no, line in enumerate(p.readlines())])
打造只在文件中包含至少兩個整數,並以零結束行的列表,使用正則表達式:
import re
p = re.compile(r'^((\-?\d*\s+){2,})0$')
with open(filename, 'rb') as f:
seq = [line.strip() for line in f if p.match(line)]
你沒有提供所有的細節,但我假設:
我不得不假設你的文件可能非常大,所以在內存中讀取整個文件,或者將整個結果列表存儲在內存中並不是一個好主意。
下面是一個快速解決方案,它逐行讀取文件並使用生成器將每行作爲列表生成。您可以使用整個結果作爲一個列表,如果你想,像這樣:
result_list = read_data('foo.dat')
,或者你可以做我的例子調用沒有和使用的每個結果行,因爲它的讀出。如果你在linux上,你可以直接調用這個文件,否則只要將它與python解釋器聯繫起來,並用數據文件的名稱作爲第一個參數來調用它,它會一行一行地打印結果 - 這將會即使您的文件很龐大,也可以工作。您也可以將該文件作爲模塊導入,並使用read_data方法並在其他計算中使用結果。
請注意,它會進行一些錯誤檢查(標題行以ap開頭,數據行以0結尾,並且只包含整數),並且您可能要麼根本不檢查,要麼提出正確的當他們遇到異常。
#!/usr/bin/env python
import sys
def read_data(fn):
"""Reads in datafile
data file is in format:
p wfgh 1111 11111 111111
287 48 0
65626 -1818 0
4654 21512 02020 0
where first line begins with p and is a header, and following lines
are comprised of at least 2 integers plus a tailing 0.
Pass in the filename, the resulting list of lists of integers will be
returned.
"""
f = open(fn, 'r')
# check for header line
assert(f.readline().split()[0]=='p')
for l in f:
d = [int(col) for col in l.split()]
if not d:
# skip empty lines
continue
# check we have at least 2 integers and the last column is 0
assert(d[-1] == 0 and len(d) >= 3)
# yield current line
yield d[:-1]
if __name__ == '__main__':
for l in read_data(sys.argv[1]):
print unicode(l)
with open('"input.txt"') as f:
lines = f.read().splitlines()
這會給你的值(字符串)列表,你在你的文件了,剝去換行。
您是否需要將每一行分開維護(如矩陣)或者只是將所有項目都列在一個列表中? – 2009-12-15 13:43:17