473,325 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

file to nested list

31
I'm having alot of troubles with this...so basically i am given a file for example:

File =

>123
qwerty
>567
tyuiyuy
>987
poiuyt

and basically the output is supposed to be [[">123" , "qwerty"] , [">567" , "tyuiuy"] , ["987" , "poiuyt"]]

i have no idea how to do that can someone help me please?.
Mar 16 '09 #1
12 3813
I'm assuming you know basic file I/O?

Basically, what you need to do is create an empty list (let's call it 'root'), open the file you want with open() (let's call this 'file'), then have a loop where you get two lines at a time using file.readline() (store these in two separate variables, 'a' and 'b' for example). Then you need to do root.append([a, b]), where [a, b] will end up being [">123" , "qwerty"] from your example. When there are no more lines to read, stop the loop, and there you have your list (root).

Does that make any sense?
Mar 16 '09 #2
v13tn1g
31
@musicfreak
yep that makes a lot of sense, and i understand fully what your trying to say except i forgot to say that the file could also be

file =

>123
qwerty
ioweio
>567
tyuiyuy
>987
poiuyt

output = [[">123" , "qwertyioweio"] , [">567" , "tyuiuy"] , ["987" , "poiuyt"]]

the output for >123 is "qwertyioweio" because for simplicity's sake i made the file look that way to look simple, so basically after "qwerty" it went to a new line because it was full. i hope that made sense....

also is there a way to use a for loop for this question?
Mar 16 '09 #3
Oh I see. Well in that case, you're going to need to make a temporary list for each "sub list," and if the line starts with '>', make that line the first part of the list, and vice versa. Then just append that list to the 'root'.

You could use a for loop, although it would be easier not to. Is this for an assignment? If you have to use a for loop, then you could read the file all at once into a string and split() it by newlines, and then loop through each line (for line in lines:, or something like that).

EDIT: Actually, now that I think about it, the way I just mentioned would probably be simpler haha.
Mar 16 '09 #4
v13tn1g
31
is your way by using a while loop?...=S(which i hate)..
Mar 16 '09 #5
Yeah, the first method I mentioned would use a while loop (it was the first thing that popped into my head since that's how I learned it), but I guess using a for loop would, in the end, be much simpler and cleaner. Go for the 'for' loop. :)
Mar 16 '09 #6
bvdet
2,851 Expert Mod 2GB
Let us assume that you have read the file using file object method readlines().
Expand|Select|Wrap|Line Numbers
  1. data = ['>123\n', 'qwerty\n', 'ioweio\n', '>567\n', 'tyuiyuy\n', '>987\n', 'poiuyt\n']
  2.  
  3. output = []
  4.  
  5. for item in data:
  6.     if item.startswith('>'):
  7.         output.append([item.strip(), ''])
  8.     else:
  9.         output[-1][1] += item.strip()
  10.  
  11. print output
Output:
>>> [['>123', 'qwertyioweio'], ['>567', 'tyuiyuy'], ['>987', 'poiuyt']]
>>>
Mar 16 '09 #7
v13tn1g
31
i think im close...can you debug my code

Expand|Select|Wrap|Line Numbers
  1. import urllib
  2.  
  3. def qwerty(f):
  4.  
  5.     a = urllib.urlopen(f)
  6.     s = ""
  7.     l = a.readlines()
  8.     y = s.join(l)
  9.     #p = y.replace('\r', "")
  10.     q = y.split('\r')
  11.  
  12.     output = [] 
  13.  
  14.     for item in q: 
  15.         if item.startswith('>'): 
  16.             output.append([item.strip(), '']) 
  17.         else: 
  18.             output[-1][1] += item.strip() 
  19.  
  20.     print output 
and the output that comes out when i test it with

Expand|Select|Wrap|Line Numbers
  1. qwerty("http://www.utsc.utoronto.ca/~szamosi/a20/assignments/a3/starter/sequences.txt")
is it will give me the whole thing as a list within a list, it doesnt seperate the numbers within...because in you "data" string the \n appears after the string whereas in mine it appears before..i think =s
Mar 16 '09 #8
bvdet
2,851 Expert Mod 2GB
You have made it too complicated. There is no need to join, split, or replace. String method strip() will remove all leading and training whitespace. You created an open file object, but never closed it. You should try to make your variable names more descriptive instead of using single letters. In my example, variable data is comparable to your variable l.

HTH
Mar 16 '09 #9
v13tn1g
31
ah ok now i see...thanks for your repliess!!
Mar 16 '09 #10
boxfish
469 Expert 256MB
@bvdet
Sorry, bvdet, but read this. How about calling it file_lines instead?
Mar 16 '09 #11
bvdet
2,851 Expert Mod 2GB
@boxfish
That would work. I sometimes name a variable of short duration, such as an open file object (f) or loop counter (i), with a single letter. Stringing several in a row makes the code unreadable. I might do something like this:
Expand|Select|Wrap|Line Numbers
  1. def qwerty(fn): # fn for file name
  2.  
  3.     f = urllib.urlopen(fn) # **EDIT**
  4.     fList = a.readlines() # or lineList or line_list or lines
  5.     f.close()
Mar 17 '09 #12
boxfish
469 Expert 256MB
Yeah, all my loop variables are called i, j, k, l, m, n, o, etc. It makes my nested loops unreadable.
Edit:
Wow, urllib is a built in library I can use! I had no idea.
Mar 18 '09 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Brett | last post by:
I am trying to pass the value of a nested list into a function (to my "if" statement) as follows: textfilelist = "]] def idfer(listlength, comparelistlength, list): while x < (listlength - 1):...
4
by: Lee K. Seitz | last post by:
I'm still relatively new to stylesheets. I'm trying to do something that seemed fairly simple on the surface, but is proving to be a challenge. I have a set of nested lists: <ul> <li>Side...
11
by: mlf | last post by:
Is there a clever way, or any way for that matter, to have a nested list show 1.1, 1.2, 1.3...2.1, 2.2, etc?
3
by: CAM | last post by:
Hi, Does anyone know a simple way to create an .xml file containing a nested list from a set of cascading relations. In other words if I have tables for Catalogue, Section, Page, Product. ...
2
by: neildunn | last post by:
Hey guys: >>> >>> def a(): .... print .... >>> a() Traceback (most recent call last): File "<stdin>", line 1, in ?
2
by: alexandre_irrthum | last post by:
Hello, I'd like to apply a function to elements of a nested list and wondered if there is anything more idiomatic and/or shorter than this recursive way: >>> def recur_map(f, data): .... if...
6
by: deko | last post by:
How do I construct an XHTML-compliant nested unordered list? This displays correctly (both FF and IE): <ul> <li>list item</li> <li>list item</li> <li>list item</li> <ul> <li>nested list...
7
by: patrick j | last post by:
Hi I'm wondering about lists with nested lists as one does on a Saturday afternoon. Anyway below is an example of a list with a nested list which the iCab browser's very useful HTML...
2
by: Gentr1 | last post by:
Hi everybody! I am presently working on a Genetic Programming API in python. I have a bit of a problem at the moment... For some specific reasons, I am using nested lists data structure to...
3
by: suneelkn | last post by:
Unable to identify the same level for nested lists in all scenarios, when the nested-list inside an ordered list the conversion process executes with out proper list order for nested list items. The...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.