Priyx
Priyx

Reputation: 73

How to write to csv file from a text file

I have text file with data like below:

"id":0
"value1":"234w-76-54"
"id":1
"value1":"2354w44-7-54"

I want to have these data in csv file. I tried with below code, but this is writing each ids and value1 as list in csv file.

with open("log.txt", "r") as file:
    file2 = csv.writer (open("file.csv", "w", newline=""), delimiter=",")
    file2.writerow(["id", "value1"])
        for lines in file:
            if "id" in lines:
                ids = re.findall(r'(\d+)', lines)
            if "value1" in lines:
                value1 = re.findall(r'2[\w\.:-]+', lines)
                file2.writerow([ids, value1])
            

getting output-

 id         value1
['0']   ['234w-76-54']
['1']   ['2354w44-7-54']

Desired output-

  id         value1
  0        234w-76-54
  1        2354w44-7-54


       

Upvotes: 1

Views: 1732

Answers (3)

patrickjlong1
patrickjlong1

Reputation: 3823

The simplest way to do it, in my opionion, is to read in the .txt file using pandas's read_csv() method and write out using the Dataframe.to_csv() method.

Below I've created a fully reproducible example recreating the OP's .txt file, reading it in and then writing out a new .csv file.

import pandas as pd

#Step 0: create the .txt file
file = open("file.txt","w")
input = '''"id":0
"value1":"234w-76-54"
"id":1
"value1":"2354w44-7-54"'''

file.writelines(input)
file.close() 

#Step 1: read in the .txt file and create a dataframe with some manipulation to 
#        get the desired shape
df = pd.read_csv('file.txt', delimiter=':', header=None)
df_out = pd.DataFrame({'id': df.loc[df.iloc[:, 0] == 'id'][1].tolist(),
'value1': df.loc[df.iloc[:, 0] == 'value1'][1].tolist()})
print(df_out)

#Step 2: Create a .csv file
df_out.to_csv('out.csv', index=False)

Expected Outputted .csv file:

Expected .csv output

Upvotes: 1

AnPla
AnPla

Reputation: 32

If your log.txt really has that simple structure you can work with split():

import csv

with open("text.txt", "r") as file:
    file2 = csv.writer(open("file.csv", "w", newline=""), delimiter=",")
    file2.writerow(["id", "value1"])
    for line in file:
        if "id" in line:
            ids = int(line.split(":")[-1])
        else:
            value = line.split(":")[-1].split('"')[1]
            file2.writerow([ids, value])

The resulting csv will contain the following:

id,value1
0,234w-76-54
1,2354w44-7-54

The comma as separator is set by the delimiter argument in the csv.writer call.

First look for a line with "id" in it. This line can easily be splitted at the :. This results in a list with two elements. Take the last part and cast it into an integer.

If no "id" is in the line, it is an "value1" line. First split the line at the :. Again take the last part of the resulting list and split it at ". This results again in a list with three elements, We need the second one.

Upvotes: 0

Tushar Sadhwani
Tushar Sadhwani

Reputation: 565

findall returns a list. you probably want to use re.search or re.match depending on your usecase.

Upvotes: 0

Related Questions