简体   繁体   中英

Python split and find specific string from a text file

I have a raw data in a .txt file format and would like to convert it to .csv file format.

This is a sample data from the txt fle:

(L2-CR666 Reception Counter) L2-CR666 Reception Counter has been forced.
(L7-CR126 Handicapped Toilet) L7-CR126 Handicapped Toilet has been forced.

I would like to achieve the following result:

L2-CR666 Reception Counter, forced
L7-CR126 Handicapped Toilet, forced

I have tried the following code but was unable to achieve the stated result. Where did I went wrong?

import csv

with open('Converted Detection\\Testing 01\\2019-02-21.txt') as infile, open('Converted Detection\\Converted CSV\\log.csv', 'w') as outfile:
    for line in infile:
        outfile.write(infile.read().replace("(", ""))
        for line in infile:
            outfile.write(', '.join(infile.read().split(')')))
            outfile.close()

You can try this :

with open('Converted Detection\\Testing 01\\2019-02-21.txt') as infile, open('Converted Detection\\Converted CSV\\log.csv', 'w') as outfile:
    for line in infile:
        # Get text inside ()
        text = line[line.find("(")+1:line.find(")")]
        # Remove \r\n        
        line = line.rstrip("\r\n")
        # Get last word
        forcedText = line.split(" ")[len(line.split(" "))-1]
        # Remove . char
        forcedText = forcedText[:len(forcedText)-1]
        outfile.write(text+", "+forcedText+"\n")

    outfile.close()

Best

You could use .partition() to truncate everything before ) and then simply replace the parts you do not want accordingly. Also, you do not have to close the file when using the with statement as it automatically closes it for you, and you do not have to import the csv library to save a file with the .csv extension.

The following code outputs your wanted result:

infile_path = "Converted Detection\\Testing 01\\2019-02-21.txt"
outfile_path = "Converted Detection\\Converted CSV\\log.csv"

with open(infile_path, "r") as infile, open(outfile_path, "+w") as outfile:
    for line in infile:
        line = line.partition(")")[2].replace(" has been forced.", ", forced").strip()
        outfile.write(line + "\n")

First for loop is reading infile. No need to reread infile and second loop. Also with block will take care of closing files.

for line in infile:
    line = line.replace("(", "")
    outfile.write(', '.join(line.split(')')))

I would suggest using:

lineout = ', '.join(linein.replace('(','').replace(')','').split(' has been ')

where:

linein = line.strip()

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