Reputation: 821
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
Upvotes: 0
Views: 57
Reputation: 169032
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
Upvotes: 2
Reputation: 135
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.
Upvotes: -1