[英]How can I add in a list some items from a text file?
我正在設置腳本,我需要從文本文件中獲取一些值。
文本文件的體系結構為:
ABC;
XYZ
1 2
3 4;
DEF;
XYZ
7 8
9 10
11 12;
GHI;
目的是獲得以下輸出:
values_list = ['XYZ 1 2 3 4', 'XYZ 7 8 9 10 11 12']
以便將其寫入我將創建的新文本文件中。
我已經試過了:
my_file = open(file, 'r')
content = my_file.read()
line = my_file.readline()
if line.startwith('XYZ'):
values_list.append(line)
但這顯然不起作用,但我沒有找到一種轉換事實的方法,以便在XYZ
之后的所有行中添加列表。
嘗試使用:
print(list(map(str.split, content.split(';')[1::2][:-1])))
輸出:
[['XYZ', '1', '2', '3', '4'], ['XYZ', '7', '8', '9', '10', '11', '12']]
如果要整數:
print([i[:1] + list(map(int, i[1:])) for i in list(map(str.split, content.split(';')[1::2][:-1]))])
輸出:
[['XYZ', 1, 2, 3, 4], ['XYZ', 7, 8, 9, 10, 11, 12]]
使用正則表達式
例如:
import re
with open(filename) as infile:
data = infile.read()
result = [" ".join(i.splitlines()).strip(";") for i in re.findall(r"([A-Z]+(?![;A-Z]).*?)[A-Z]+;", data)] #Regex Help --> https://stackoverflow.com/a/21709242/532312
print(result)
輸出:
['XYZ 1 2 3 4', 'XYZ 7 8 9 10 11 12']
您可以遍歷各行,並連接XYZ
行之后的行,並在此過程中進行一些字符串處理:
In [48]: with open('file.txt') as f:
...: out = []
...: text = ''
...: for line in f:
...: if line.startswith('XYZ'):
...: text = 'XYZ'
...: elif text.startswith('XYZ') and line.startswith(' '):
...: text += line.rstrip(';\n')
...: else:
...: if text:
...: out.append(text)
...: text = ''
...:
In [49]: out
Out[49]: ['XYZ 1 2 3 4', 'XYZ 7 8 9 10 11 12']
使用re
:
data = '''ABC;
XYZ
1 2
3 4;
DEF;
XYZ
7 8
9 10
11 12;
GHI;'''
import re
out = [re.sub(r'\n|;', '', g, flags=re.M) for g in re.split(r'^\w+;', data, flags=re.M) if g.strip()]
print(out)
印刷品:
['XYZ 1 2 3 4', 'XYZ 7 8 9 10 11 12']
簡短的正則表達式方法:
import re
with open(file.txt') as f:
content = f.read()
repl_pat = re.compile(r'\s+')
values = [repl_pat.sub(' ', s.group())
for s in re.finditer(r'\bXYZ\s+[^;]+', content, re.M)]
print(values)
輸出:
['XYZ 1 2 3 4', 'XYZ 7 8 9 10 11 12']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.