简体   繁体   English

Python拆分并从文本文件中查找特定的字符串

[英]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. 我有一个.txt file格式的原始数据,并想将其转换为.csv file格式。

This is a sample data from the txt fle: 这是来自txt文件的示例数据:

(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. 您可以使用.partition()在之前截断所有内容,然后简单地替换不需要的部分。 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. 另外,使用with语句时也不必关闭文件,因为它会自动为您关闭文件,也不必导入csv库来保存扩展名为.csv的文件。

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. 首先,for循环是读取infile。 No need to reread infile and second loop. 无需重新读取infile和第二个循环。 Also with block will take care of closing files. 另外, with block会关闭文件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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