473,387 Members | 1,365 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,387 software developers and data experts.

Read from file into dictionary

if this was my input file:
Expand|Select|Wrap|Line Numbers
  1. AATGC
  2. AGGC
  3. 0.0
  4. 0.0
  5. 4
  6. ATGC
  7. 4
  8. ATGC
  9. 1 1 A A 1
  10. 1 2 A T 0
  11. 1 3 A G 0
  12. 1 4 A C 0
  13. 2 1 T A 0
  14. 2 2 T T 1
  15. 2 3 T G 0
  16. 2 4 T C 0
  17. 3 1 G A 0
  18. 3 2 G T 0
  19. 3 3 G G 1
  20. 3 4 G C 0
  21. 4 1 C A 0
  22. 4 2 C T 0
  23. 4 3 C G 0
  24. 4 4 C C 1
  25.  
How could I generate a dictionary using the last 16 lines? I would like to combine the two letters and use them as a key, and then us the last int as the value stored at the key.

This is supposed to be a scoring matrix. If there is a better way to do this, by all means please help me be more efficient.

there will be files that have ints longer than one digit. So i tried to dump the contents into a list and iterate/take slices, but it did not work.

Please help!
May 16 '12 #1
1 1924
Expand|Select|Wrap|Line Numbers
  1. result = dict()
  2.  
  3. with open('filename.txt', 'r') as source_file:
  4.     for line in source_file.readlines():
  5.         parts = line.split()
  6.         if len(parts) < 5: continue
  7.         key = '{0}{1}'.format(parts[2], parts[3])
  8.         result[key] = parts[4]
  9.  
  10. print result
May 17 '12 #2

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

Similar topics

4
by: domtam | last post by:
Suppose I have one of those USB storage devices (like a mp3 player, USB thumbdrive, or even digital camera) connected to my computer. I'd like to write a C# program that can - detect that the...
40
by: Abby | last post by:
My .dat file will contain information like below. /////////// First 0x04 0x05 0x06 Second 0x07
2
by: somequestion | last post by:
During copying file , wanna read file Size like this string CheckFileSize(string fileName) { if( fileName == null ) return; FileInfo fi = new FileInfo(fileName); return fi.Length.ToString();...
1
by: potluri040 | last post by:
hi, could any one let me know how to read file thru VB script. Scenerio is like this: i am keeping a personal details such as name, age, sex, company, location in a text file called user details...
1
by: samira | last post by:
hi all, i built aproject to my client he need that the visitors to the site can read file from his computer, how can i do this , we uuse asp.net over c#
5
by: rohit | last post by:
i want to implement a dictionary in python the query is : without explicitly doing the following steps 1. reading from file to list 2. processing list to dictionary is there a module or a built...
3
by: =?Utf-8?B?Sm9obiBXYWxrZXI=?= | last post by:
Hi, Is there anything wrong with the code below in sending my browser page to Excel? Before my page opens in Excel there's a message "Problems came up in the following areas during load:" and it...
2
by: xplode144 | last post by:
I have a Web application. i need to read a file once during the startup and preserve the read data throughout the life of the application. i will to access the data often during the page_load of...
2
by: danimian | last post by:
Hello, first i am creating xml file if file does not exist. String myFile = "C:\myxmlfile.xml"; if (!File.Exists(myFile)) { using (FileStream conStream = new FileStream(myFile,...
0
by: leeamiin | last post by:
Hi, i need help with bellow file format, i work for telecom company and i'm the developer, what i need help with is to read file with bellow format, as you can see the file has { and , as...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.