在python中导入文本文件

时间:2013-10-15 21:29:01

标签: python file import

我是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])

1 个答案:

答案 0 :(得分:4)

此?

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