在python中导入文本文件

use*_*657 3 python import file

我对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])
Run Code Online (Sandbox Code Playgroud)

Ryf*_*lex 5

这个?

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

print data
Run Code Online (Sandbox Code Playgroud)

或者

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

print data
Run Code Online (Sandbox Code Playgroud)

或者

python script.py
Run Code Online (Sandbox Code Playgroud)

或者

python script.py text.txt
Run Code Online (Sandbox Code Playgroud)

或者

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

print csv_reader
Run Code Online (Sandbox Code Playgroud)

有人不清楚的帖子:S