2013-10-15 21 views
3

我對python非常陌生。我只想知道我們將在哪裏導入文件,我們想在下面的python腳本中運行一個腳本。我只想導入或引用下面的腳本中的文件在Python中導入一個文本文件

import fileinput, optparse 

usage = """%prog HOCOMOCOv9_AD_TRANSFAC.txt > converted_transfac_matrices.txt 

This program reads matrices in a format used by the TRANSFAC database, 
and writes them in a format that can be used by Clover. The TRANSFAC 
format looks something like this: 

AC M00002 
XX 
P0  A  C  G  T 
01  4  4  3  0  V 
02  2  5  4  0  S 
03  3  2  4  2  N""" 


op = optparse.OptionParser(usage=usage) 
(opts, args) = op.parse_args() 
if not args: op.error("please specify an input file") 

title = [] 

for line in fileinput.input(args): 
    w = line.split() 
    key = w[0] 
    if key in ("AC", "ID", "NA"): 
     title.extend(w[1:]) 
    elif key.isdigit(): 
     if title: 
      print ">" + " ".join(title) 
      title = [] 
     print "\t".join(w[1:5]) 
+0

我很困惑 - 完全**你想做什麼**? –

+0

我只是找不到在上面的腳本中導入我的文本文件的方法。我想在文本文件上運行這個腳本。我不知道如何引用或導入特定的文本文件。這是我第一次在做python – user2498657

+0

你的意思是你想讀*一個文本文件? –

回答

3

This?

with open('file.txt','r') as f_open: 
    data = f_open.read() 

print data 

f_open = open('file.txt','r') 
data = f_open.read() 
f_open.close() 

print data 

python script.py 

python script.py text.txt 

import csv 
with open('file.csv', 'rb') as open_csv: 
    csv_reader = csv.reader(open_csv) 

print csv_reader 

有人不清楚的帖子:S