简体   繁体   中英

reading txt file with multiple delimeters

I have a txt file with seven columns and no headers . There is a space separating the first two columns and then semicolons serve as the delimiter thereafter.

Here is what I tried before realizing the file wasn't uniform:

 min17_q1 = open('ES 03-17.Last.txt', 'r')
 for line in min17_q1:
    fields = line.split(";")
    Date = fields[0]
    Time = fields[1]
    Open = fields[2]
    High = fields[3]
    Low = fields[4]
    Close = fields[5]
    Volume = fields[6]
    print(DateTime + Open + High + Low + Close + Volume)

How can I read this file in and then be able to manipulate the data and/or append identical files to this one?

To parse one such line from your file, split on the first space with str.split(maxsplit=1) , then split again on semicolons:

for line in min17_q1:
    Date, fields = line.split(" ", 1)
    Time, Open, High, Low, Close, Volume = fields.split(';')
    print(Date + Time + Open + High + Low + Close + Volume)

To concatenate multiple of these files, use a loop:

with open('output.txt') as outfile:
    # iterate over the files that need to be concatenated
    for infile in ['min17_q1', 'min17_q2', 'min17_q3']:
        with open(infile) as infile:
            # parse each line in the file and add it to the output file
            for line in infile:
                line = line.replace(' ', ';', 1)
                outfile.write(line)

In this case it's not even necessary to split each row into a list. We can just replace the first space in the line with a semicolon and write it back. To do this, I used str.replace(' ', ';', count=1) .

This can be done with a single split:

for line in min17_q1:
    Date, Time, Open, High, Low, Close, Volume = line.replace(' ',';',1).split(';')
    print(Date + Time + Open + High + Low + Close + Volume)

and the reverse operation:

line = "%s %s;%s;%s;%s;%s;%s" % (Date, Time, Open, High, Low, Close, Volume)
import re,os

outputfile = open('outfile.txt','a')

for all_files in os.listdir(os.getcwd()):
        if re.match(r'samp.*',all_files):
                min_file = open(all_files, 'r')
                for line in min_file:
                        fields=re.split(r'[ ;]',line.strip())
                        c=';'.join(fields)
                        #print c
                        outputfile.write(c)
                        outputfile.write('\n')
                min_file.close()
outputfile.close()

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