Python Forum
String to List question help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String to List question help
#3
This is a neat way of doing it. the difficulty is that I am importing the data directly from my machine through GPIB and its importing as a str "type" and I am not actually calling a CSV file. So Im stuck at being able to seperate the string into seperate columns. The screen shot of the data I show is how the Python imports the data directly from the machine.

(Sep-05-2023, 06:45 PM)deanhystad Wrote: Please post text, not screenshots. When you post text, others can easily copy/paste to create data for their own testing.

It looks like this would be easy using pandas.

I mocked up some data that looks something like yours. Tab delimited values with some lines at the top that need to be skipped, and an annoying "!" before the column headers.
Output:
# 34589043! FILE NAME ! DATE NOW ! RAW DATA # More information ! FREQ S11M S21M S21A 0.11444405920970335 0.22851792934978143 0.16724464908750036 0.054813079444560264 0.11565032195951042 0.23010933442970138 0.3048773751796404 0.11465260170716174 0.20102541854079847 0.27830101316888567 0.3995954975408248 0.12034618280172404 0.41276142607996513 0.31147127777115224 0.46636510024384636 0.2806783250819618 0.495356229079312 0.3495931528852747 0.6389401268977468 0.38079631999232333 0.556074773658631 0.3894689120388013 0.6779475794713076 0.43728550829612134 0.6332585933784733 0.4924761223011904 0.7503465368809027 0.7386632480838207 0.6341059239183447 0.5130383410762516 0.8554371206142789 0.7695854491274398 0.8902822004610119 0.7219019187548961 0.8712110079483135 0.8443884202719537 0.9170577193238573 0.7970927214052331 0.9344766589022325 0.8597198838156557
This code reads the file to extract the column names, then uses pandas to read the file into a dataframe.
import pandas as pd

# Read column names from 4th line in file.  Remove leading "!"
with open("data.csv", "r") as file:
    columns = file.readlines()[4][1:].split()

# Read file into dataframe.  Skip the first 5 rows and use the column
# names from above.
df = pd.read_csv("data.csv", sep="\t", skiprows=5, names=columns)
print(df)
Output:
FREQ S11M S21M S21A 0 0.114444 0.228518 0.167245 0.054813 1 0.115650 0.230109 0.304877 0.114653 2 0.201025 0.278301 0.399595 0.120346 3 0.412761 0.311471 0.466365 0.280678 4 0.495356 0.349593 0.638940 0.380796 5 0.556075 0.389469 0.677948 0.437286 6 0.633259 0.492476 0.750347 0.738663 7 0.634106 0.513038 0.855437 0.769585 8 0.890282 0.721902 0.871211 0.844388 9 0.917058 0.797093 0.934477 0.859720
Now it is easy to get the columns.
print(df["FREQ"])
Output:
0 0.114444 1 0.115650 2 0.201025 3 0.412761 4 0.495356 5 0.556075 6 0.633259 7 0.634106 8 0.890282 9 0.917058 Name: FREQ, dtype: float64
Reply


Messages In This Thread
String to List question help - by James_Thomas - Sep-05-2023, 03:52 PM
RE: String to List question help - by deanhystad - Sep-05-2023, 06:45 PM
RE: String to List question help - by James_Thomas - Sep-06-2023, 09:14 AM
RE: String to List question help - by Pedroski55 - Sep-06-2023, 10:59 AM
RE: String to List question help - by deanhystad - Sep-06-2023, 12:22 PM
RE: String to List question help - by James_Thomas - Sep-06-2023, 01:17 PM
RE: String to List question help - by deanhystad - Sep-06-2023, 02:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  syntax error question - string mgallotti 5 1,507 Feb-03-2023, 05:10 PM
Last Post: mgallotti
Big Grin General programming question (input string)[ jamie_01 2 1,722 Jan-08-2022, 12:59 AM
Last Post: BashBedlam
  Question about change hex string to integer sting in the list (python 2.7) lzfneu 1 2,633 May-24-2021, 08:48 AM
Last Post: bowlofred
  Question on HTML formatting with set string in message Cknutson575 3 3,642 Mar-09-2021, 08:11 AM
Last Post: Cknutson575
  question: finding multiple strings within string djf123 4 3,113 May-16-2020, 01:00 PM
Last Post: snippsat
  Simple newb string question Involute 2 2,338 Sep-08-2019, 12:50 AM
Last Post: Involute
  string parsing question ( I think) diggee17 4 3,152 Jul-24-2019, 02:37 PM
Last Post: diggee17
  I converted string to 'list', but it doesn't look like a list! mrapple2020 3 3,375 Apr-07-2019, 02:34 PM
Last Post: mrapple2020
  please help with this question about using operators to multiply a string? GilesTwigg 3 4,542 Feb-27-2019, 04:13 PM
Last Post: ichabod801
  about List question longmail 2 2,487 Nov-30-2018, 02:08 AM
Last Post: longmail

Forum Jump:

User Panel Messages

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