Text Files

Thread Solved

Join Date: Apr 2009
Posts: 30
Reputation: Joe Hart is an unknown quantity at this point 
Solved Threads: 0
Joe Hart Joe Hart is offline Offline
Light Poster

Text Files

 
0
  #1
Sep 25th, 2009
I have a text file that I need to find data and assign it to a variable. I have found code to read and search a file but the information I want is not always constant. The Items in Red will change. Is there a way to find this information, maybe by position and assign it to a variable? EX.FCITC = 690.4

Sorry if I was suppose to wrap the text files with the code tags I was not sure, because it was not code.
Thanks,
Joe

Study transfer. From FMPP_EXA To FPL_IMA . Transfer level - 1000.0 MW


Violations report ordered by transfer capability. Total 3 violations


FCITC TDF LODF <--------- Limiting constraint ---------> <--------- Contingency description ---------> Ncon PreShift Rating PTDF IntBase FnlBase
690.4 -0.12312 - 5567 IND RIV 115 B$0029 CTAB GSU 1.00 1 Base Case 0.0 -85.0

Study transfer level - 1000.0 MW. Total violations: 21
First violation - -756.5 MW.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 12
Reputation: The-IT is an unknown quantity at this point 
Solved Threads: 1
The-IT The-IT is offline Offline
Newbie Poster

Re: Text Files

 
0
  #2
Sep 25th, 2009
just bu looking at your post, i could not tell what you actualy wanted, however, here is code that will open a file and put its contents into a variable named data:
  1. fileName = raw_input('What is the file name? ')
  2. file = open(fileName, 'w') #'w', means if the file does not exsist, python will create a new one with nothing in it.
  3. data = file.read() #use readlines() if you you prefer a list with each line.
hope that helped
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 49
Reputation: willygstyle is an unknown quantity at this point 
Solved Threads: 6
willygstyle willygstyle is offline Offline
Light Poster

Re: Text Files

 
0
  #3
Sep 26th, 2009
  1. #opens a test file to write data to
  2. newfile = open("test.py", "wt")
  3. newfile.write("TXRGP = 370.5\n")
  4. newfile.write("GPYBN = 666\n")
  5. newfile.write("FCITC = 690.4\n")
  6. newfile.close()
  7.  
  8. #read the file and search for a string
  9. #strip of any unwanted charecters
  10. newfile = open("test.py", "rt")
  11. for line in newfile:
  12. if "FCITC" in line:
  13. x = line[6:].strip("=")
  14. x = x.strip(" ")
  15. else:
  16. pass
  17. newfile.close()
  18. print(x)
Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 30
Reputation: Joe Hart is an unknown quantity at this point 
Solved Threads: 0
Joe Hart Joe Hart is offline Offline
Light Poster

Re: Text Files

 
0
  #4
Sep 26th, 2009
I am not sure if I made my self clear in the previous post or maybe I just don't understand the code. The number I want is the FCITC, the Limiting Constraints and the Contingency that caused the Limiting Constraint. These variables will constantly change and therefore I can not search for them in the file so I will have to find them based on location or maybe some other method. In the example I posted these variables I would like are 690.4, 5567 IND RIV 115, AND Base Case, but if I ran this again these values will probably change. I would like to store them as variables because I would like to use them in various logic schemes and or excel files.

The format of the text files should not change. Using Ultra edit the values line up nice in columns but I am not sure what the delimiter is. This is my first post using the bb code so please let me know if it looked correct. I don't necessarily want anyone to write the code for but to tell me what functions to use would be great, although I am not against someone writing the code either it would make it easier to understand. Below is the full text file I will be using. Hopefully it looks right I tried to bold the correct Items but I am not used to this text editor.

Thanks again,
Joe
[TEX]
PSS(R)MUST 9.2 -- Managing and Utilizing System Transmission -- THU, SEP 24 2009 15:13
2008 FRCC LFDB - 90% LOAD
2009 SUMMER BASE
Case.File C:\HourlyATC\ATC090924\OPCASE12.sav
Subsys.File C:\HourlyATC\subsystem.sub
Monit.File C:\HourlyATC\monitor.mon
Contin.File C:\HourlyATC\contingency.con
Exclud.File C:\HourlyATC\exclude.exc

Study transfer level - 1000.0 MW. Total violations: 3
First violation - 690.4 MW.

Study transfer. From FMPP_EXA To FPL_IMA . Transfer level - 1000.0 MW


Violations report ordered by transfer capability. Total 3 violations


FCITC TDF LODF <--------- Limiting constraint ---------> <--------- Contingency description ---------> Ncon PreShift Rating PTDF IntBase FnlBase
690.4 -0.12312 - 5567 IND RIV 115 B$0029 CTAB GSU 1.00 1 Base Case 0.0 -85.0

Study transfer level - 1000.0 MW. Total violations: 21
First violation - -756.5 MW.

Study transfer. From FMPP_EXA To PEF_IMA . Transfer level - 1000.0 MW


Violations report ordered by transfer capability. Total 21 violations


FCITC TDF LODF <--------- Limiting constraint ---------> <--------- Contingency description ---------> Ncon PreShift Rating PTDF IntBase FnlBase
-756.5 -0.04682 0.30400 6106 I-STATE 230 6104 TENOROC 230 1 C: 7890 OSCEOLA 230 9190 AGNES-RX 230 1 415 -472.4 -437.0 -0.05872 -314.6 -270.2
Open 7890 OSCEOLA 230 9190 AGNES-RX 230 1

Study transfer level - 1000.0 MW. Total violations: 25
First violation - 589.3 MW.

Study transfer. From FMPP_EXA To TECO_IMA . Transfer level - 1000.0 MW


Violations report ordered by transfer capability. Total 25 violations


FCITC TDF LODF <--------- Limiting constraint ---------> <--------- Contingency description ---------> Ncon PreShift Rating PTDF IntBase FnlBase
589.3 0.08324 -0.36819 5704 TAFT 230 5710 C CENTER 230 1 C: 2883 INTERCSN 230 5352 CAN ISL 230 1 393 317.9 367.0 0.06531 173.5 212.0
Open 2883 INTERCSN 230 5352 CAN ISL 230 1

Study transfer level - 1000.0 MW. Total violations: 7
First violation - 1905.9 MW.

Study transfer. From FPL_EXA To FMPP_IMA . Transfer level - 1000.0 MW


Violations report ordered by transfer capability. Total 7 violations


FCITC TDF LODF <--------- Limiting constraint ---------> <--------- Contingency description ---------> Ncon PreShift Rating PTDF IntBase FnlBase
1905.9 -0.15360 -0.75247 6101 MCINTOSH 230 6104 TENOROC 230 1 C: 9100 RECKER 230 9150 LKAGNES 230 1 416 -144.2 -437.0 -0.09545 128.5 -53.4
Open 9100 RECKER 230 9150 LKAGNES 230 1

Study transfer level - 1000.0 MW. Total violations: 18
First violation - 1119.9 MW.

Study transfer. From PEF_EXA To FMPP_IMA . Transfer level - 1000.0 MW


Violations report ordered by transfer capability. Total 18 violations


FCITC TDF LODF <--------- Limiting constraint ---------> <--------- Contingency description ---------> Ncon PreShift Rating PTDF IntBase FnlBase
1119.9 0.10692 0.51805 2167 WINDERME 230 5701 SO WOOD 230 1 C:5353-5800 582 247.3 367.0 0.08738 114.1 212.0
Open 5352 CAN ISL 230 5800 OUCCITP1 230 1

Study transfer level - 1000.0 MW. Total violations: 910
First violation - -1121.7 MW.

Study transfer. From TECO_EXA To FMPP_IMA . Transfer level - 1000.0 MW


Violations report ordered by transfer capability. Total 910 violations


FCITC TDF LODF <--------- Limiting constraint ---------> <--------- Contingency description ---------> Ncon PreShift Rating PTDF IntBase FnlBase
-1121.7 -0.06399 -0.13767 467 POINSETT 230 2882 HOLOPAW 230 1 C: 351 ORANGE R 230 354 ORANGE R 500 1 200 -661.8 -590.0 -0.04773 -518.2 -464.6
Open 351 ORANGE R 230 354 ORANGE R 500 1

Study transfer level - 1000.0 MW. Total violations: 11
First violation - 1776.3 MW.

Study transfer. From IPP_REL_EXA To FMPP_IMA . Transfer level - 1000.0 MW


Violations report ordered by transfer capability. Total 11 violations


FCITC TDF LODF <--------- Limiting constraint ---------> <--------- Contingency description ---------> Ncon PreShift Rating PTDF IntBase FnlBase
1776.3 -0.41356 0.92486 461 CAPE K 230 5703 IND RIV 230 2 C: 461 CAPE K 230 5703 IND RIV 230 1 361 -62.4 -797.0 -0.21569 -32.6 -415.7
Open 461 CAPE K 230 5703 IND RIV 230 1

Study transfer level - 1000.0 MW. Total violations: 16
First violation - 899.9 MW.

Study transfer. From IPP_REL_EXA To FPL_IMA . Transfer level - 1000.0 MW


Violations report ordered by transfer capability. Total 16 violations


FCITC TDF LODF <--------- Limiting constraint ---------> <--------- Contingency description ---------> Ncon PreShift Rating PTDF IntBase FnlBase
899.9 0.05451 -0.36819 5704 TAFT 230 5710 C CENTER 230 1 C: 2883 INTERCSN 230 5352 CAN ISL 230 1 393 317.9 367.0 0.04730 173.5 216.1
Open 2883 INTERCSN 230 5352 CAN ISL 230 1

Study transfer level - 1000.0 MW. Total violations: 27
First violation - -1670.7 MW.

Study transfer. From IPP_REL_EXA To PEF_IMA . Transfer level - 1000.0 MW


Violations report ordered by transfer capability. Total 27 violations


FCITC TDF LODF <--------- Limiting constraint ---------> <--------- Contingency description ---------> Ncon PreShift Rating PTDF IntBase FnlBase
-1670.7 -0.03205 -0.56485 6106 I-STATE 230 6104 TENOROC 230 1 C: 2878 BARCOLA 230 6102 WEST 230 1 389 -490.5 -437.0 -0.03885 -314.6 -249.7
Open 2878 BARCOLA 230 6102 WEST 230 1

Study transfer level - 1000.0 MW. Total violations: 25
First violation - 593.8 MW.

Study transfer. From IPP_REL_EXA To TECO_IMA . Transfer level - 1000.0 MW


Violations report ordered by transfer capability. Total 25 violations


FCITC TDF LODF <--------- Limiting constraint ---------> <--------- Contingency description ---------> Ncon PreShift Rating PTDF IntBase FnlBase
593.8 0.08260 -0.36819 5704 TAFT 230 5710 C CENTER 230 1 C: 2883 INTERCSN 230 5352 CAN ISL 230 1 393 317.9 367.0 0.07257 173.5 216.6
Open 2883 INTERCSN 230 5352 CAN ISL 230 1
/TEX]
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 49
Reputation: willygstyle is an unknown quantity at this point 
Solved Threads: 6
willygstyle willygstyle is offline Offline
Light Poster

Re: Text Files

 
0
  #5
Sep 26th, 2009
Ok, I think I got ya now. Yeah thats gonna take a bit better scheme all in itself to extract those variables. You are most likely going to want to take a look at the re module which I just started using myself. It allows you to setup regular expressions so you can setup patterns that it will pick up on. Some of the obvious patterns I'm seeing so far are, the first variable always starts at the beggining of your third line down. The next seems to be between a float value and the integer 230 most the time. The hard part is making sure you have setup your regular expression to account for all of these minor details. That should at least give you a direction to move in for what you are working on.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,523
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 169
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Text Files

 
1
  #6
Sep 27th, 2009
See if something like this will do, but be careful that all the slicing doesn't steal some data:
  1. def extract_number(data_str):
  2. """
  3. extract the numeric value from a string
  4. (the string should contain only one numeric value)
  5. return the numeric part of the string or None
  6. """
  7. s = ""
  8. for c in data_str:
  9. if c in '1234567890.-':
  10. s += c
  11. if s:
  12. return s
  13. else:
  14. return None
  15.  
  16.  
  17. def extract_between(text, sub1, sub2):
  18. """
  19. extract a substring from text between first
  20. occurances of substrings sub1 and sub2
  21. """
  22. return text.split(sub1, 1)[-1].split(sub2, 1)[0]
  23.  
  24.  
  25. # test file
  26. fname = "violate.dat"
  27.  
  28. mylist = []
  29. flag = False
  30. for line in open(fname):
  31.  
  32. if flag == True:
  33. mylist.append(line)
  34. flag = False
  35. if "FCITC" in line:
  36. flag = True
  37.  
  38. newlist = []
  39. for item in mylist:
  40. temp1 = item[:16]
  41. #print(temp1) # test
  42. temp1 = extract_number(temp1)
  43.  
  44. temp2 = item[35:83]
  45. #print(temp2) # test
  46. temp2 = temp2[6:28].replace(']', ' ')
  47. temp2 = temp2.strip()
  48. #print(temp2) # test
  49.  
  50. temp3 = item[96:144]
  51. #print(temp3) # test
  52. temp3 = extract_between(temp3, '[B]', '[/B]').rstrip()
  53. #print(temp3) # test
  54.  
  55. newlist.append((temp1, temp2, temp3))
  56.  
  57.  
  58. print('-'*40)
  59.  
  60. for tup in newlist:
  61. print(tup)
  62.  
  63. """my show -->
  64. ('690.4', '5567 IND RIV 115', 'Base Case')
  65. ('-756.5', '6106 I-STATE 230', '7890 OSCEOLA 230 9190 AGNES-RX')
  66. ('589.3', '5704 TAFT 230', ' 2883 INTERCSN 230 5352 CAN ISL')
  67. ('1905.9', '6101 MCINTOSH 230', ' 9100 RECKER 230 9150 LKAGNES')
  68. ('1119.9', '2167 WINDERME 230', '5353-5800')
  69. ('-1121.7', '467 POINSETT 230', '351 ORANGE R 230 354 ORANGE R')
  70. ('1776.3', '461 CAPE K 230', '461 CAPE K 230 5703 IND RIV')
  71. ('899.9', '5704 TAFT 230', ' 2883 INTERCSN 230 5352 CAN ISL')
  72. ('-1670.7', '6106 I-STATE 230', 'BARCOLA 230 6102 WEST')
  73. ('593.8', '5704 TAFT 230', '2883 INTERCSN 230 5352 CAN ISL')
  74. """
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 30
Reputation: Joe Hart is an unknown quantity at this point 
Solved Threads: 0
Joe Hart Joe Hart is offline Offline
Light Poster

Re: Text Files

 
0
  #7
Sep 28th, 2009
I will give this a try. Also can you tell me why after your name in the Quote field their are numbers. I am new to the BBcode and I am not sure if I am doing it right. jlim699 said he had to rewrite my text file so that others could read it but I am not sure what I did wrong or how he changed it. Sorry it has taken me a while to respond this weekend was my son's 1st birthday so I was busy.
Thanks again,
Joe
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 30
Reputation: Joe Hart is an unknown quantity at this point 
Solved Threads: 0
Joe Hart Joe Hart is offline Offline
Light Poster

Re: Text Files

 
0
  #8
Sep 28th, 2009
Hi Ene Uran,
Looking over the code it looks like it would work, but I can't get it to run. When you open the file, which in my case would be, C:\HourlyATC\viols.lis, you don't include the path, just the name "violate.dat". How does this work? I looked up "open" and usually you would tell the command where the file is located and use a 'r' or 'w' so I am not sure how yours works. It might take me a while to go through the whole code, becaues their are a lot of functions I have never used before.
Thanks,
Joe
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,523
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 169
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Text Files

 
0
  #9
Sep 28th, 2009
That is because I made a test file from your posting and put it into the working directory, the same directory the code file is in.

Since you have the real thing you can replace
fname = "violate.dat"
with
fname = r"C:\HourlyATC\viols.lis"
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 30
Reputation: Joe Hart is an unknown quantity at this point 
Solved Threads: 0
Joe Hart Joe Hart is offline Offline
Light Poster

Re: Text Files

 
0
  #10
Sep 28th, 2009
I figured that out, and it runs with now errors but I still can't see the results. I think it has something to do with I don't know how to run visual studio to get the output.
Joe
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC