简体   繁体   中英

how to read the content of .txt file using python?

output_filename = r"C:\Users\guage\Output.txt"
RRA:
GREQ-299684_6j 
GREQ-299684_6k 
CZM:
V-GREQ-299684_6k 
V-GREQ-299524_9 
F_65624_1 
R-GREQ-299680_5 
DUN:
FB_71125_1 
FR:
VQ-299659_18 
VR-GREQ-299659_19 
VEQ-299659_28 
VR-GREQ-299659_31 
VR-GREQ-299659_32 
VEQ-299576_1 
GED:
VEQ-299622_2 
VR-GREQ-299618_13 
VR-GREQ-299559_1 
VR-GREQ-299524_14
FB_65624_1 
VR-GREQ-299645_1 
MNT:
FB_71125_1 
FB_71125_2 
VR-534_4 

The above is the content of the the .txt file. how can I read it separately the content of it. for example -

RRA:VR-GREQ-299684_6j VR-GREQ-299684_6k VR-GREQ-299606_3 VR-GREQ-299606_4 VR-GREQ-299606_5 VR-GREQ-299606_7 

and save it in a variable or something similar to it. Later I want to read CZM separately and so on. I did as below.

with open(output_filename, 'r') as f:
        excel = f.read()

But how to read it separately ? can someone tell me how to do it ?

Something like this:

def read_file_with_custom_record_separator(file_path, delimiter='\n'):
    fh = open(file_path)
    data = ""
    for line in fh:
        if line.strip().endswith(delimiter) and data != "":
            print "VARIABLE:\n<", data, ">\n"
            data = line
        else:
            data += line
    print "LAST VARIABLE:\n<", data, ">\n"

And then:

read_file_with_custom_record_separator("input.txt", ":")

You can make use of the file text : as indicator to create a new file like this:

savefilename = ""
with open(filename, 'r') as f:
    for line in f:
        line = line.strip() # get rid of the unnecessary white chars
        lastchar = line[-1:] # get the last char
        if lastchar == ":": # if the last char is ":"
            savefilename = line[0:-1] # get file name from line (except the ":")
            sf = open(savefilename + ".txt", 'w') # create a new file
        else:
            sf.write(line + "\n") # write the data to the opened file

Then you should get collection of files:

RRA.txt
CZM.txt
DUN.txt
# etc

which contains all the appropriate data:

RRA.txt

VR-GREQ-299684_6j
VR-GREQ-299684_6k
VR-GREQ-299606_3
VR-GREQ-299606_4
VR-GREQ-299606_5
VR-GREQ-299606_7

CZM.txt

VR-GREQ-299684_6k
VR-GREQ-299606_6
VR-GREQ-299606_8
VR-GREQ-299640_1
VR-GREQ-299640_5
VR-GREQ-299524_9
FB_65624_1
VR-GREQ-299680_5

DUN.txt

FB_71125_1

# and so on

You can replace the sf = open and the sf.write which whatever way you feel best to separate the data. Here, I use files...

You can iterate over the file and use the lines and indices to your advantage; something like this:

with open(output_filename, 'r') as f:
    for index, line in enumerate(f):
        # here you have access to each line and its index
        # so you can save any number of lines you wish

What about reading it into a list, then process its element as you prefer

>>> f = open('myfile.txt', 'r').readlines()
>>> len(f)
46
>>> f[0]
RRA:

>>> f[-1]
VR-GREQ-299534_4

>>> f[:3]
['RRA:\n', 'VR-GREQ-299684_6j \n', 'VR-GREQ-299684_6k \n']
>>>
>>> [l for l in f if l.startswith('FB_')]
['FB_65624_1 \n', 'FB_71125_1 \n', 'FB_69228_1 \n', 'FB_65624_1 \n', 'FB_71125_1 \n', 'FB_71125_2 \n']
>>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM