LinuxQuestions.org
Review your favorite Linux distribution.
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 03-08-2012, 11:14 AM   #1
ArthurHuang
Member
 
Registered: Jan 2006
Posts: 174

Rep: Reputation: 30
Python parse text question


Hi there, I am quite new to python; I usually use C/C++ to do the following job,but it looks more and more tedious.
I want to learn python by this try out, please help.

I want to convert the following format:

Key1 Key2 key3
1 2 3
3 2 4
2 2 3
Duplicated Key1 Duplicated Key1 Duplicated Key1
5 2 5
4 2 5
...
...

To a clean format:

Key1 Key2

1 2

2 2

3 2

4 2

5 2
....
....

There are 3 things need to implement:
1) Remove the duplicated key1, key2, key3 in the middle of text.
2) Truncate the key3 column.
3) Sort the whole file by key1

How can I make it? Thanks a lot!

thanks a lot!
 
Old 03-08-2012, 11:44 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
What have you tried? Python is an extensive language and a simple google will show a plethora of examples and tutorials.
 
Old 03-08-2012, 11:29 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
check the CSV file reading function. You can use that to get your data into a list and then you can manipulate the list to get the data into the format that you require.
 
Old 03-08-2012, 11:40 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,235

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Quote:
Originally Posted by ArthurHuang View Post
1) Remove the duplicated key1, key2, key3 in the middle of text.
This is the easiest way to remove duplicates:

Code:
>>>list(set([1, 2, 2]))
[1, 2]
Quote:
2) Truncate the key3 column.
Code:
>>>[1, 2, 3][:-1]
[1, 2]
Quote:
3) Sort the whole file by key1
After parsing the file into a list of lists or a list of tuples:

Code:
>>>>sorted([[1, 2, 3], [3, 2, 1], [2, 3, 1]], key=lambda x: x[0])
[[1, 2, 3], [2, 3, 1], [3, 2, 1]]

Last edited by dugan; 03-08-2012 at 11:42 PM.
 
  


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
Python - How to get text file data into an array on python. golmschenk Programming 4 11-11-2013 09:15 AM
How to Parse the file correctly with Python andrapgm03 Programming 3 11-28-2010 08:11 PM
Python Parse Code snowman81 Programming 3 01-24-2009 10:50 AM
How to parse text file to a set text column width and output to new text file? jsstevenson Programming 12 04-23-2008 02:36 PM
Parse Python through use of .htaccess Hal Linux - General 1 01-24-2006 12:33 AM

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

All times are GMT -5. The time now is 02:06 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