分析具有公共和不同属性的制表符分隔的.txt文件

我想解析选项卡分隔.txt文件,将常见属性和不同属性与文件分开。我只想解析第一行属性而不是值。您能纠正此脚本吗?该文件可以从此URL-
ftp://ftp.ebi.ac.uk/pub/databases/mi...mx-10.sdrf.txt
我编写的源代码如下 -

选择 | 换行 | 行号
  1. #!/usr/bin/python
  2. import glob
  3. outfile = open('output_attribute.txt' , 'w')
  4. files = glob.glob('*.sdrf.txt')
  5. for file in files:
  6.     infile = open(file)
  7.     #ret = False
  8.     for line in infile:
  9.         lineArray = line.split('\t')
  10.  
  11.         if '\n\n' in line:
  12.             ret = false
  13.             outfile.write('')
  14.             break;
  15.         elif len(lineArray) > 2:            
  16.            output = "%s\t%s\n\n"%(lineArray[0],lineArray[1])
  17.            outfile.write(output)
  18.         else:
  19.             output = "%s\t\n"%(lineArray[0])
  20.             outfile.write(output)
  21.     infile.close()
  22. outfile.close()
# 回答1


我不清楚您的最终目标。看来您想读取多个文件,读取每个文件的第一行,在选项卡字符上拆分行,然后将前两个元素写入输出文件。您能澄清您想要的输出吗?
# 回答2


亲爱的,
请找到所附的zip文件。我只想从解析的文件中提取标题。标题中的每个文件都以数组设计名称开头,但没有修复属性。因此,我想提取带有空间(\ n \ n)分离间隙的标题,该间隙以zip文件格式附加。我只想提取红色的标头。我很高兴您的支持与合作。
带着敬意,
Haobijam

附加的文件

File Type: zip

标题

(73.7 KB,159次观看)

# 回答3


我只想从解析的文件中提取标题。标题中的每个文件都以数组设计名称开头,但以Unfix属性结尾。因此,我想提取带有空间(\ n \ n)分离间隙的标题,该间隙以zip文件格式附加。我只想提取红色的标头。我已经附上了本脚本的输出。我很高兴您的支持与合作。
该文件可以从此URL-
ftp://ftp.ebi.ac.uk/pub/databases/mi...ffy-10.adf.txt
我写的源代码如下 -

选择 | 换行 | 行号
  1. #!/usr/bin/python
  2. import glob
  3.  
  4. outfile = open('output_attri.txt' , 'w')
  5. files = glob.glob('*.adf.txt')
  6.  
  7. for file in files:
  8.     infile = open(file)
  9.  
  10.     for line in infile:
  11.         line = line.replace('^' , '\n\n').replace('!' , '').replace('#' , '').replace('\n','')
  12.         lineArray = line.split('%s\t')
  13.         if line == '\n\n':
  14.             outfile.write('')
  15.             break;
  16.         elif len(lineArray) > 2:            
  17.             output = "%s\t%s\n"%(lineArray[0],lineArray[1])
  18.             outfile.write(output)
  19.         else:
  20.             output = "%s\t\n"%(lineArray[0])
  21.             outfile.write(output)
  22.     infile.close()
  23. outfile.close()

带着敬意,
Haobijam

附加的文件

File Type: zip

output_attribute.zip

(2.46 MB,125次观看)

# 回答4


是否总是有一条空白行,将所需的标头信息与您不需要的数据分开?您只想要每个标头线的前两个元素吗?未经测试:

选择 | 换行 | 行号
  1. outFile = open(outFileName, 'w')
  2. for fn in fileNameList:
  3.     f = open(fn)
  4.     output = []
  5.     for line in f:
  6.         line = line.strip().split("\t")
  7.         if line:
  8.             output.append("\t".join(line[:2]))
  9.         else:
  10.             outFile.write("\n".join(output))
  11.             break
  12. outFile.close()
# 回答5


亲爱的,
是的,总是有一个空白行,将我想要的标题信息从我不想在所有文件中提取的文本数据中分开。
问候,
Haobijam
# 回答6


亲爱的,
这个脚本怎么了?我无法打印任何输出。

选择 | 换行 | 行号
  1. #!/usr/bin/python
  2. import glob
  3.  
  4. outFile = open('output.txt', 'w')
  5. fileNameList = glob.glob('*.adf.txt')
  6. for file in fileNameList:
  7.     f = open(file)
  8.     output = []
  9.     for line in f:
  10.         line = line.strip().split("\t")
  11.         #lineArray = line.split('\t')
  12.         if line:
  13.             #output = "%s\t%s\n"%(lineArray[0],lineArray[1])
  14.             output.append("\t".join(line[:2]))
  15.         else:
  16.             outFile.write("\n".join(output))
  17.             break
  18.     f.close()
  19. outFile.close()

代码在这里 -

# 回答7


发布代码时,请使用代码标签。这样,我将不必编辑您的帖子。
没有打印语句。输出文件中有任何内容吗?添加打印语句,如
打印行
,看看正在阅读什么。
BV-主持人
# 回答8


这将标题信息写入磁盘:

选择 | 换行 | 行号
  1. outFile = open(outFileName, 'w')
  2. for fn in fileNameList:
  3.     f = open(fn)
  4.     output = []
  5.     for line in f:
  6.         line = line.strip()
  7.         if line:
  8.             output.append(line)
  9.         else:
  10.             outFile.write("\n".join(output))
  11.             f.close()
  12.             break
  13. outFile.close()
  14.  
# 回答9


请注意,由于将其视为两个单独的记录,因此永远不会找到他的。测试LEN(Line.Strip())而不是查找空记录。

选择 | 换行 | 行号
  1.         if '\n\n' in line:
# 回答10


尊敬的先生,
我已经写了一个脚本 提取以源名称开头的第一行,并以注释[arrayExpress数据检索URI]结束,我做到了,但是我无法解析每个文件中未重复的独特或唯一属性。我想仅解析第一行属性而不是表值。您能纠正此脚本吗?我已经为所有sdrf.txt文件附加了一个zip文件。该文件可以从此URL-
ftp://ftp.ebi.ac.uk/pub/databases/mi...fmx-1.sdrf.txt

选择 | 换行 | 行号
  1.  

问候,
Haobijam

附加的文件

File Type: zip

sdrf.txt.zip

(95.7 kb,78次观点)

File Type: txt

sdrf.txt

(536个字节,417个视图)

File Type: zip

output_att.zip

(3.0 kb,93次观点)

# 回答11

选择 | 换行 | 行号
  1. #!/usr/bin/python
  2. import glob
  3. #import linecache
  4. outfile = open('output_att.txt' , 'w')
  5. files = glob.glob('*.sdrf.txt')
  6. for file in files:
  7.     infile = open(file)
  8.     #count = 0
  9.     for line in infile:
  10.  
  11.         lineArray = line.rstrip()
  12.         if not line.startswith('Source Name') : continue
  13.         #count = count + 1
  14.         lineArray = line.split('%s\t')
  15.         print lineArray[0]
  16.         output = "%s\t\n"%(lineArray[0])
  17.         outfile.write(output)
  18.     infile.close()
  19. outfile.close() 
  20.  
# 回答12


尊敬的先生,
我想从所有sdrf.txt文件中提取唯一的术语,但是此Python代码分别为每个文件输出唯一项。像数组数据文件一样,大多数sdrf.txt文件中都重复数组设计参考...,因此我不想将其打印为唯一的术语。您能告诉我在python中隐藏案例敏感的,因为特征[有机体]被印刷为特征[有机体]的独特术语[有机部分],对于特征[性别]的特征[性别] [sexs]。我热切地等待您的支持和积极的答复。

选择 | 换行 | 行号
  1. #!/usr/bin/python
  2. import glob
  3. import string
  4.  
  5. outfile = open('output.txt' , 'w')
  6. files = glob.glob('*.sdrf.txt')
  7. previous = set()
  8. for file in files:
  9.     print('\n'+file)
  10.     infile = open(file)
  11.     #previous = set() # uncomment this if do not need to be unique between the files
  12.     for line in infile:
  13.         lineArray = line.rstrip()
  14.         if not line.startswith('Source Name') : continue
  15.         lineArray = line.split('%s\t')
  16.         output = "%s\t\n"%(lineArray[0])
  17.         outfile.write(output)
  18.         uniqwords = set(word.strip() for word in lineArray[0].split('\t')
  19.                         if word.strip() and word.strip() not in previous) 
  20.         print('The %i unique terms are:\n\t%s' % (len(uniqwords),'\n\t'.join(sorted(uniqwords))))
  21.         previous |=  uniqwords 
  22.     infile.close()
  23. outfile.close()
  24. print('='*80)
  25. print('The %i terms are:\n\t%s' % (len(previous),'\n\t'.join(sorted(previous))))
  26.  

附加的文件

File Type: zip

sdrf.zip

(95.7 kb,77次观点)

File Type: zip

attribute.zip

(2.9 kb,108次观看)

# 回答13


尊敬的先生,
我确实有一个有关解析属性的查询,并从arrayexpress中提取adf.txt文件的唯一术语[
ftp://ftp.ebi.ac.uk/pub/databases/mi...y/data/array/]
。此处编写的Python代码对于运行具有类似起始学期的单个文件是可行的,但一次在2270 ADF.TXT文件左右运行是不可行的。您能否纠正或建议我在第12行中的此Python代码的一些提示。实际上,我想为每个adf.txt文件(数字2270)解析第一行,然后从中提取唯一的术语和常见条款。为了方便起见,我已为ADF.TXT格式附加了一个ZIP文件,但是您可能会进入上面提到的FTP网站。我很高兴您的支持与合作。
温暖的问候,
Haobijam

选择 | 换行 | 行号
  1. #!/usr/bin/python
  2. import glob
  3. import string
  4. with open('output_Reporter Name.txt' , 'w') as outfile:
  5.     files = glob.glob('*.adf.txt')
  6.     uniqwords = set()
  7.     previous = set()
  8.     for file in files:
  9.         with open(file) as infile:
  10.             #previous = set() # uncomment this if do not need to be unique between the files
  11.             for line in infile:
  12.                 if not line.startswith('Reporter Name') : continue ## change this line to deal with other form
  13.                 output = line
  14.                 uniqwords = set(word.strip() for word in line.rstrip().split('\t')
  15.                                 if word.strip() and word.strip() not in previous)
  16.                 previous |=  uniqwords
  17.                 print (output)
  18.                 outfile.write(output)
  19. print('The %i unique terms are:\n\t%s' % (len(uniqwords),'\n\t'.join(sorted(uniqwords))))                  
  20. print('='*80)
  21. print('The %i terms are:\n\t%s' % (len(previous),'\n\t'.join(sorted(previous))))
  22.  

附加的文件

File Type: zip

adf.zip

(1.01 MB,107视图)

标签: python

添加新评论