Python Forum
beginner text formatting single line to column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
beginner text formatting single line to column
#1
Hello, I'm brand new to python having worked through an online python course for beginners, but with no other experience. For my first project I've been tasked with what I assume is a pretty simple task, but that is going right over my head. I'm usually pretty good at tracking down examples or tutorials that can get me where I need to go, but this has got me. I am two days into online searches and tutorials, none of which seem to be what I need or getting me any closer to finding a path forward. Any help or direction this group could provide would be very helpful.

Essentially, I have a .txt file that contains a single very long string that I need to rearrange so its readable in a different program. Here is a much simplified example:

Original data file-all in a single string.
{"header1":[data1],"header2"[dataA, dataB, dataC], "header3":[dataX, dataY, dataZ], "header4":[[0, 1, 2]], "header5":[dataz]}

Needed format
#header1= data1
#header5= dataz
#data table= header5, header2, header3
0 dataA dataX
1 dataB dataY
2 dataC dataZ

As I said, any help or direction on where to look or what this type of reformatting is called would be very helpful. Thank you.
Reply
#2
I think this is not just a text file. I think it is a JSON file. Please read JSON encoder and decoder to see if this fits.
snippsat and buran like this post
Reply
#3
As mention bye ibreeden the data is JSON or can already been converted to a Python dictionary.
Saving to text file is something that not should done as it destroy the data structure.
Can be possible to get back bye using eg eval(),but that is the wrong way.

So here some fix to make work an can then can take it into Pandas to easier get format wanted.
# Some fix to so it work as a dictionary 
>>> d = {"header1":['data1'], "header2":['dataA', 'dataB', 'dataC'], "header3":['dataX', 'dataY', 'dataZ'], "header4":[[0, 1, 2]], "header5":['dataz']}
>>> d
{'header1': ['data1'],
 'header2': ['dataA', 'dataB', 'dataC'],
 'header3': ['dataX', 'dataY', 'dataZ'],
 'header4': [[0, 1, 2]],
 'header5': ['dataz']}

>>> import pandas as pd
>>> 
# Load into Pandas,orient change so it fill(None) for missing values 
>>> df = pd.DataFrame.from_dict(d, orient='index')
>>> df
                 0      1      2
header1      data1   None   None
header2      dataA  dataB  dataC
header3      dataX  dataY  dataZ
header4  [0, 1, 2]   None   None
header5      dataz   None   None
>>> 
# Transpose index to columns 
>>> df.transpose()
  header1 header2 header3    header4 header5
0   data1   dataA   dataX  [0, 1, 2]   dataz
1    None   dataB   dataY       None    None
2    None   dataC   dataZ       None    None
Reply
#4
Thanks for the pointers. It was a json file originally.
Reply
#5
Thank you all very much, with your help I've made considerable progress and learned a lot. I have opened the JSON directly and moved it into a dictionary. However, I'm still doing something wrong with the conversion from the dictionary to the dataframe as it will not transpose the data.

#Build dictionary
d={"freq":[FREQ], "sw":[sw], "ref":[ref], "spec":[spec], "WholeEcho":[WE], "Time":[T], "dataReal":[RD], "dataImag":[ID]}

#import pandas
import pandas as pd

# Load into Pandas,orient change so it fill(None) for missing values
df = pd.DataFrame.from_dict(d, orient='index')

#transpose index to columns
df.T
print(df)

FREQ, sw, spec, and WholeEcho are all floats. ref is a string, and T, RD, and ID are lists. My intention is that T, RD, and ID are lists of floats, but I'm not sure if I was successful.

The output is:

0
freq 1.23456e+08
sw 10000
ref [NAN]
spec 0
WholeEcho 0
Time [[0.00, 0.01, 0.02, 0.03, 0.04, 0.05]]
dataReal [[4, 6, 8, 9, 10, 12]]
dataImag [[1, 2, 3, 5, 7, 11]]

But ideally what I'd like to see in the output is...

freq 1.23456e+08
sw 10000
ref [NAN]
spec 0
WholeEcho 0

Time dataReal dataImag
0.00 4 1
0.01 6 2
0.02 8 3
0.03 9 5
0.04 10 7
0.05 12 11

Thank you for the help and direction.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to do next line output from CSV column? atomxkai 2 428 Oct-02-2021, 01:00 AM
Last Post: Pedroski55
Lightbulb Multiple inputs on the same line (beginner) dementshuk 9 491 Sep-03-2021, 02:21 PM
Last Post: dementshuk
  Presenting multiline data into single line aaronbuhu 1 372 Aug-05-2021, 10:57 AM
Last Post: jamesaarr
  Want to remove the text from a particular column in excel shantanu97 2 491 Jul-05-2021, 05:42 PM
Last Post: eddywinch82
  [Solved] Reading every nth line into a column from txt file Laplace12 7 955 Jun-29-2021, 09:17 AM
Last Post: Laplace12
  Ploting single column with multiple category drunkenneo 1 710 May-26-2021, 04:51 PM
Last Post: jefsummers
  Importing text file into excel spreadsheet with formatting david_dsmn 1 726 Apr-05-2021, 10:21 PM
Last Post: david_dsmn
  Remove single and double quotes from a csv file in 3 to 4 column shantanu97 0 2,258 Mar-31-2021, 10:52 AM
Last Post: shantanu97
  how to connect mysql from txt 1 line goes good but not all lines in text kingceasarr 4 849 Mar-24-2021, 05:45 AM
Last Post: buran
  Text to Column to Excel in Python giddyhead 0 603 Feb-09-2021, 03:18 AM
Last Post: giddyhead

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020