在python中导入文本文件

2024-05-29 10:10:04 发布

您现在位置: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])

Tags: 文件keyintxt脚本formatiftitle
1条回答
网友
1楼 · 发布于 2024-05-29 10:10:04

这个?

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

相关问题 更多 >

    热门问题