Hakim Bajuri
Hakim Bajuri

Reputation: 61

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()

Upvotes: 1

Views: 504

Answers (4)

Mary
Mary

Reputation: 89

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")

Upvotes: 2

Maxouille
Maxouille

Reputation: 2911

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

Upvotes: 2

msi_gerva
msi_gerva

Reputation: 2078

I would suggest using:

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

where:

linein = line.strip()

Upvotes: 0

Frane
Frane

Reputation: 534

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(')')))

Upvotes: 1

Related Questions