LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-05-2014, 05:39 AM   #1
simhumcon
Member
 
Registered: Dec 2013
Posts: 35

Rep: Reputation: Disabled
Parsing a table using python


hi All,

i have this below data from a file :
RxDqLeft RxDqRight RxVLow RxVHigh TxDqLeft TxDqRight TxVLow TxVHigh
--------------------------------------------------------------------------------
N1.C0.D0.R0: -17 15 -23 21 -19 20 -13 14
N1.C0.D0.R1: -17 16 -23 22 -19 19 -15 14
N1.C1.D0.R0: -16 17 -23 23 -12 15 -22 18
N1.C3.D0.R0: -14 14 -23 23 -12 14 -21 18

using python i want to create a script that will check this data whereby, any if any number in line N1.C0.D0.R0 is below then 10, its a failure. i want to the same on all lines. can someone please help me? i have no clue on where to start
 
Old 07-05-2014, 06:51 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
what have you tried and where are you stuck ?
 
Old 07-05-2014, 06:54 AM   #3
simhumcon
Member
 
Registered: Dec 2013
Posts: 35

Original Poster
Rep: Reputation: Disabled
Code:
file_location = "C:\\users\\20609\\Desktop\\lalala.txt"

with open(file_location) as f :
    for line in f :
        if "N" in line :
            polyshape=[]
            var = line.split()
            print var
            var = [int(i) for i in var]
            polyshape.append(var)
this is what i've tried. im trying to read the numbers and make them as a variable so that later i can say : if var < int(10), make it fail

Last edited by simhumcon; 07-05-2014 at 07:00 AM.
 
Old 07-05-2014, 10:21 AM   #4
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Note this line:

Code:
var = [int(i) for i in var]
However, your target line has a field that is not an integer (the first field namely, or var[0] in your script). So these line will likely raise an exception. You can fix this by using array slicing to refer to only the fields in the array you actually want to process.
 
1 members found this post helpful.
Old 07-05-2014, 12:15 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
this worx for me
Code:
#!/usr/bin/python

with open("simhumcon.txt") as f:
 for line in f :
  print "-----schneidz: line = " + line
  for v in line.split():
   print "-----schneidz: v = " + v
   if "N1.C0.D0.R0:" in v:
    print "-----schneidz: enter if"
    for vx in line[12:].split():
     print "-----schneidz: for loop vx = " + vx
     if int(vx) < 10:
      print "error"

Last edited by schneidz; 07-05-2014 at 12:17 PM.
 
Old 07-06-2014, 04:07 AM   #6
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

You may consider splitting line first on ':' and then splitting second part (to the right of ':') on spaces.

Something like this (no error checking, just dirty quick example)
Code:
line = 'N1.C0.D0.R0: -17 15 -23 21 -19 20 -13 14'
arr = line.split(':')[1].split()
is_all_gt_10 = all(map(lambda x: int(x)>10, arr))  # False
And, yes, list comprehensions are a preferrable way to deal with such things in python, I just personally prefer map etc.

Last edited by firstfire; 07-06-2014 at 04:09 AM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] python parsing nitrohuffer2001 Programming 7 04-01-2014 02:43 PM
parsing table using linux commands upendra_35 Linux - Newbie 6 11-30-2012 10:52 AM
[SOLVED] Parsing S-Expressions in Python MTK358 Programming 2 02-02-2011 08:00 AM
Parsing table with Hpricot hattori.hanzo Programming 1 11-26-2010 06:59 AM
Python: Parsing data in chunks/sections? Neruocomp Programming 3 11-06-2010 04:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration