Separate Text File into Two Lists Using Python


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Separate Text File into Two Lists Using Python
# 1  
Old 03-03-2013
Separate Text File into Two Lists Using Python

Hello, I have a pretty simple question, but I am new to Python and am trying to write a simple program. Put simply, I want to take a text file that looks like this:
Code:
11111 22222
33333 44444
55555 66666
77777 88888

and produce two lists, one containing the contents of the left column, one the contents of the right one, that should look like this:
List1
Code:
11111
33333
55555
77777

List2
Code:
22222
44444
66666
88888

I tried this code, and several variations of it:
Code:
# Read runlist into two separate lists, "data_list" and "flasher_list"
data_list=[]
flasher_list=[]
file = open(runlist) #"runlist" is previously declared filepath
for line in file:
    data_list+=line[0:5]
    flasher_list+=line[6:11]

But, not surprisingly, it outputs something like:
Code:
['1', '1', '1', '1', '1', '3', '3', '3', '3', '3', '5', '5', '5', '5', '5', '7', '7', '7', '7', '7']

and
Code:
['2', '2', '2', '2', '2', '4', '4', '4', '4', '4', '6', '6', '6', '6', '6', '8', '8', '8', '8', '8']

Like I said I'm new to Python. I'm sure there's some class or module that can help, just not sure. Any ideas?

---------- Post updated 03-03-13 at 12:30 AM ---------- Previous update was 03-02-13 at 09:44 PM ----------

Pretty sure I figured it out:
Code:
data_list = []
flasher_list = []
file = open(runlist)
list = file.readlines()
file.close()
for index in list:
    data_list.append(index.split()[0])
    flasher_list.append(index.split()[1])

This seems to do the trick.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh / AIX - Differences between lists to a text file

This seems pretty simple, but I cant figure it out. I get stumped on the simple things. I am running two commands 1) take a listing a directory of files, and filter out the doc_name (which is in a series of extracted files), and place it in a file. ls -l | awk '{print $9}' | grep... (5 Replies)
Discussion started by: jeffs42885
5 Replies

2. Programming

creating separate output file for each input file in python

Experts, Need your help for this. Please support My motive is to create seperate output file for each Input Files(File 1 and File2) in another folder say(/tmp/finaloutput) Input files File 1(1.1.1.1.csv) a,b,c 43,17104773,3 45,17104234,4 File 2(2.2.2.2.csv) a,b,c 43,17104773,1... (2 Replies)
Discussion started by: as7951
2 Replies

3. UNIX for Beginners Questions & Answers

Ls to text file on separate lines

hi, I'm trying to print out the contents of a folder into a .txt file. The code I'm trying amongst variations is: ls -1 > filenames.txt but it prints them all on the same line ie. image102.bmpimage103.bmpimage104.bmpimage105.bmpimage106.bmp how can I change this? Please... (2 Replies)
Discussion started by: newbie100
2 Replies

4. Programming

Python Concatenating 2 Lists

I am just trying to concatenate two lists together, but I am not sure what is wrong with my code, it won't run. Thank you for any help. #!/usr/bin/python # Takes two lists and returns a list that is the concatenation of both of # them. A = B = def list_concat( A, B): print(A) ... (2 Replies)
Discussion started by: totoro125
2 Replies

5. Programming

Populating Lists in Def using Python

Dipping around in python again and need to create a def that will populate a list(content) with the files that os.walk finds from within this directory and then I will re.search through each files looking for content. In learning Python, can someone point me in the right direction. This is what I... (3 Replies)
Discussion started by: metallica1973
3 Replies

6. Homework & Coursework Questions

[Python] Compare 2 lists

Hello, I'm new to the python programming, and I have a question. I have to write a program that prints a receipt for a restaurant. The input is a list which looks like: product1 product3 product8 .... In the other input file there is a list which looks like: product1 coffee 5,00... (1 Reply)
Discussion started by: dagendy
1 Replies

7. Programming

Python: Compare 2 word lists

Hi. I am trying to write a Python programme that compares two different text files which both contain a list of words. Each word has its own line worda wordb wordc I want to compare textfile 2 with textfile 1, and if there's a word in textfile 2 that is NOT in textfile 1, I want to... (6 Replies)
Discussion started by: Bloomy
6 Replies

8. Shell Programming and Scripting

Splitting text file into 2 separate files ??

Hi All, I am new to this forumn as well to the UNIX, I have basic knowledge of UNIX which I studied some years ago, now I have to do some shell scripting to load data into Oracle database using sqlldr utility, whcih I am able to do. I have a requirement where I need to do following operation. I... (10 Replies)
Discussion started by: shekharjchandra
10 Replies

9. Shell Programming and Scripting

script to separate bilingual text file

Hi all I have a bilingual text file, one language is English and another one is Assamese. I want to write English language text into one file and Assamese language text into another file. For Example- input file is as dsad জন gdfkghdfkghkdf hksdjhfkjsdfhk hksjdhfksdjf আনজনৰ মনত ghgj jkj... (11 Replies)
Discussion started by: wildhorse
11 Replies

10. Shell Programming and Scripting

Separate lines from text file

I have a text file with lot of rows like.. Action & Adventure|2012: Supernova NR|2009-11-01 00:01:00|2010-05-01 23:59:00|Active|3 Action & Adventure|50 Dead Men Walking|2010-01-05 00:01:00|2010-06-30 23:59:00|Active|3 Action & Adventure|Afterwards|2009-11-26 00:01:00|2010-03-26... (3 Replies)
Discussion started by: ramse8pc
3 Replies
Login or Register to Ask a Question