0

How to convert the following input into a tab-delimited file using Python?

Input

[ 49 302 515 691 909] {'prominences': array([1.495, 1.565, 1.655, 2.3, 1.88 ]), 'left_bases': array([  8, 122, 122, 122, 122]), 'right_bases': array([122, 312, 536, 764, 932]), 'widths': array([36.93773946,  7.14150718,  7.38614341, 39.32723577,  8.17883298]), 'width_heights': array([-0.4975, -0.2275, -0.0875, -0.325 ,  0.1   ]), 'left_ips': array([ 34.52777778, 298.15454545, 510.59302326, 673.91666667,904.25490196]), 'right_ips': array([ 71.46551724, 305.29605263, 517.97916667, 713.24390244,912.43373494])}

output

enter image description here

1
  • 1
    Honestly, by finding out where that input came from and making that program output something more easily parseable, such as plain JSON. That data is a mishmash of various formats and you'll have to more or less roll a bespoke parser that may or may not break. Commented Apr 29, 2020 at 7:44

2 Answers 2

2

This is more or less terrible and will break with unexpected input, but happens to work with the data you have. Really, though, as said in my comment, get the program that data came from to do something saner!

EDIT: If your "input" is actually the output of a print(a, b) statement in your own program, then you can skip the horrible parsing below, and substitute those two values for numbers and data.

import re
import ast
import csv
import sys


def parse_horrible_data_string(s):
    numbers_string, data_string = re.match(r"^\[(.+?)\] (.+)$", s).groups()
    numbers = [int(n) for n in numbers_string.strip().split()]
    data = ast.literal_eval(data_string.replace(": array(", ": ("))
    return (numbers, data)


s = """[ 49 302 515 691 909] {'prominences': array([1.495, 1.565, 1.655, 2.3, 1.88 ]), 'left_bases': array([  8, 122, 122, 122, 122]), 'right_bases': array([122, 312, 536, 764, 932]), 'widths': array([36.93773946,  7.14150718,  7.38614341, 39.32723577,  8.17883298]), 'width_heights': array([-0.4975, -0.2275, -0.0875, -0.325 ,  0.1   ]), 'left_ips': array([ 34.52777778, 298.15454545, 510.59302326, 673.91666667,904.25490196]), 'right_ips': array([ 71.46551724, 305.29605263, 517.97916667, 713.24390244,912.43373494])}"""
numbers, data = parse_horrible_data_string(s)

data_keys = list(data.keys())
writer = csv.writer(sys.stdout)
writer.writerow(["No"] + data_keys)
for i, number in enumerate(numbers):
    row = [number] + [data[key][i] for key in data_keys]
    writer.writerow(row)

outputs

No,prominences,left_bases,right_bases,widths,width_heights,left_ips,right_ips
49,1.495,8,122,36.93773946,-0.4975,34.52777778,71.46551724
302,1.565,122,312,7.14150718,-0.2275,298.15454545,305.29605263
515,1.655,122,536,7.38614341,-0.0875,510.59302326,517.97916667
691,2.3,122,764,39.32723577,-0.325,673.91666667,713.24390244
909,1.88,122,932,8.17883298,0.1,904.25490196,912.43373494
Sign up to request clarification or add additional context in comments.

6 Comments

Is it possible to call the file from outside the script like using s = sys.argv[1]
The absolute easiest would be to replace the s = ... thing with s = sys.stdin.read() and then pipe in your file: python script.py < file.txt. Note that this won't read multi-line files.
I get this error AttributeError: 'NoneType' object has no attribute 'groups'
Yes, because your input data did not match what the code expected. You could try s = sys.stdin.read().strip() to get rid of newlines.
Glad I could help!
|
-1
 import pandas as pd
 data = list()
 # data can be a dictionary
 df = pd.Dataframe(data)
 df.to_csv('filename', sep='give tab space here')

please go through pandas to csv documentation

here you can find how to convert the pandas data frame to a csv and also can specify what delimiter you want for the data.

7 Comments

OP has a string of data that's not in a format Pandas can just ingest.
that will help in converting the data to tab seperated values, please try my code if you have doubts
you cant write such big code for a simple thing which can be done in few steps
I got you, thought it was a normal list or json input, once the data is changed to list or dict then my process works
Except that OP's string uses single quotes, which aren't valid JSON, and they have those array(...) calls which aren't valid JSON either.
|

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.