[英]Python, remove specific columns from file
我有一個以下格式的文件;
string1 string2 ........ stringN
value1,1 value1,2 ........ value1,N
. . ........ .
. . ........ .
. . ........ .
valueM,1 valueM,2 ........ valueM,N
M在10000的尺度上N在100的尺度上
我需要
從該文件分別。
numpy變得非常棘手,因為此數據中也包含字符串(每列的標題)。 我將不勝感激。
您有一種自定義的類似於ASCII表的格式,帶有固定列:
*********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
* Row * Instance * test_string * test_string * test_string * test_string * test_string * test_string * test_string * string__722 * string__722 * string__722 * string__722 * string__722 * string__722 * string__722 * string__720 * string__720 * string__720 * string__720 * string__720 * string__720 * string__720 * HCAL_SlowDa * HCAL_SlowDa * HCAL_SlowDa * HCAL_SlowDa * HCAL_SlowDa * HCAL_SlowDa * HCAL_SlowDa * string__718 * string__718 * string__718 * string__718 * string__718 * string__718 * string__718 * string__719 * string__719 * string__719 * string__719 * string__719 * string__719 * string__719 * string__723 * string__723 * string__723 * string__723 * string__723 * string__723 * string__723 * string__721 * string__721 * string__721 * string__721 * string__721 * string__721 * string__721 * another_str * another_str * another_str * another_str * another_str * another_str * another_str * another_str * another_str *
*********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
* 0 * 0 * 0 * 50331648 * test_string * 2 * 1 * 13 * 5.76460e+18 * 0 * 50331648 * string__722 * 2 * 1 * 606 * 5.83666e+18 * 0 * 50331648 * string__720 * 2 * 1 * 575 * 5.83666e+18 * 0 * 50331648 * HCAL_SlowDa * 2 * 1 * 36 * 5.76460e+18 * 0 * 50331648 * string__718 * 2 * 1 * 529 * 5.83666e+18 * 0 * 50331648 * string__719 * 2 * 1 * 529 * 5.83666e+18 * 0 * 50331648 * string__723 * 2 * 1 * 529 * 5.83666e+18 * 0 * 50331648 * string__721 * 2 * 1 * 529 * 5.83666e+18 * 0 * 50331648 * 212135 * 15080 * 1 * 1 * 3340 * 1057 * 1.399999976 *
* 0 * 1 * 0 * 50331648 * * 2 * 1 * 13 * 0 * 0 * 50331648 * * 2 * 1 * 606 * 53440 * 0 * 50331648 * * 2 * 1 * 575 * 53440 * 0 * 50331648 * * 2 * 1 * 36 * 0 * 0 * 50331648 * * 2 * 1 * 529 * 53440 * 0 * 50331648 * * 2 * 1 * 529 * 53440 * 0 * 50331648 * * 2 * 1 * 529 * 53440 * 0 * 50331648 * * 2 * 1 * 529 * 53440 * 0 * 50331648 * 212135 * * 1 * 1 * 3340 * 1057 * 1.399999976 *
* 0 * 2 * 0 * 50331648 * * 2 * 1 * 13 * 4294970636 * 0 * 50331648 * * 2 * 1 * 606 * 1.09780e+16 * 0 * 50331648 * * 2 * 1 * 575 * 1.09780e+16 * 0 * 50331648 * * 2 * 1 * 36 * 2.70217e+16 * 0 * 50331648 * * 2 * 1 * 529 * 1.09780e+16 * 0 * 50331648 * * 2 * 1 * 529 * 1.09780e+16 * 0 * 50331648 * * 2 * 1 * 529 * 1.09780e+16 * 0 * 50331648 * * 2 * 1 * 529 * 1.09780e+16 * 0 * 50331648 * 212135 * * 1 * 1 * 3340 * 1057 * 1.399999976 *
* 0 * 3 * 0 * 50331648 * * 2 * 1 * 13 * 352321545 * 0 * 50331648 * * 2 * 1 * 606 * 2.30610e+18 * 0 * 50331648 * * 2 * 1 * 575 * 2.30610e+18 * 0 * 50331648 * * 2 * 1 * 36 * 7.30102e+18 * 0 * 50331648 * * 2 * 1 * 529 * 1.15294e+19 * 0 * 50331648 * * 2 * 1 * 529 * 1.15294e+19 * 0 * 50331648 * * 2 * 1 * 529 * 1.15294e+19 * 0 * 50331648 * * 2 * 1 * 529 * 1.15294e+19 * 0 * 50331648 * 212135 * * 1 * 1 * 3340 * 1057 * 1.399999976 *
* 0 * 4 * 0 * 50331648 * * 2 * 1 * 13 * 0 * 0 * 50331648 * * 2 * 1 * 606 * 1.15294e+19 * 0 * 50331648 * * 2 * 1 * 575 * 1.15294e+19 * 0 * 50331648 * * 2 * 1 * 36 * 2.82590e+16 * 0 * 50331648 * * 2 * 1 * 529 * 1.15294e+19 * 0 * 50331648 * * 2 * 1 * 529 * 1.15294e+19 * 0 * 50331648 * * 2 * 1 * 529 * 1.15294e+19 * 0 * 50331648 * * 2 * 1 * 529 * 1.15294e+19 * 0 * 50331648 * 212135 * * 1 * 1 * 3340 * 1057 * 1.399999976 *
如果我們假設實際的數據字段本身都不包含星號,則讀取每一行的最簡單方法是使用正則表達式將行分開。
要輸出,我仍將使用csv
模塊 ,因為這將使以后的處理變得更加容易:
import csv
import re
from itertools import islice
row_split = re.compile('\s*\*\s*')
with open(someinputfile, 'rb') as infile, open(outputfile, 'wb') as outfile:
writer = csv.writer(outfile, delimiter='\t')
next(islice(infile, 3, 3), None) # skip the first 3 lines in the input file
for line in infile:
row = row_split.split(line)[1:-1]
if not row: continue
writer.writerow(row[8::7])
這將跳過空的行,並僅寫入每第7列(從第9列開始計數),並跳過其余的列。
因此,第一行是:
['5.76460e+18', '5.83666e+18', '5.83666e+18', '5.76460e+18', '5.83666e+18', '5.83666e+18', '5.83666e+18', '5.83666e+18', '3340']
這將刪除空行:
filtered = filter(lambda x: not re.match(r'^\s*$', x), original)
要刪除特定的列(我假設您的數據存儲在文本文件中):
f = open("textfile.txt","r")
lines = f.readlines()
f.close()
f = open("newfile.txt","w")
寫回您的行,但要刪除的行除外:
list = [0, 1, 6, 13, 20] # remove first,second as well as 7th, 14th and 21th line
for i,line in enumerate(lines):
if i not in list:
f.write(line)
最后,再次關閉文件。
f.close()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.