1

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


       
3
  • 1
    Ehat about using file2.writerow([ids[0], value1[0]])? Commented Apr 23, 2021 at 12:45
  • 1
    use re.search instead of re.findall Commented Apr 23, 2021 at 12:45
  • 1
    This worked, Thanks @BarbarosÖzhan Commented Apr 23, 2021 at 12:56

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.