| | |
Text Files
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2009
Posts: 30
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jun 2009
Posts: 12
Reputation:
Solved Threads: 1
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:
hope that helped
Python Syntax (Toggle Plain Text)
fileName = raw_input('What is the file name? ') file = open(fileName, 'w') #'w', means if the file does not exsist, python will create a new one with nothing in it. data = file.read() #use readlines() if you you prefer a list with each line.
•
•
Join Date: Aug 2009
Posts: 56
Reputation:
Solved Threads: 6
python Syntax (Toggle Plain Text)
#opens a test file to write data to newfile = open("test.py", "wt") newfile.write("TXRGP = 370.5\n") newfile.write("GPYBN = 666\n") newfile.write("FCITC = 690.4\n") newfile.close() #read the file and search for a string #strip of any unwanted charecters newfile = open("test.py", "rt") for line in newfile: if "FCITC" in line: x = line[6:].strip("=") x = x.strip(" ") else: pass newfile.close() print(x)
•
•
Join Date: Apr 2009
Posts: 30
Reputation:
Solved Threads: 0
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]
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]
•
•
Join Date: Aug 2009
Posts: 56
Reputation:
Solved Threads: 6
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.
See if something like this will do, but be careful that all the slicing doesn't steal some data:
python Syntax (Toggle Plain Text)
def extract_number(data_str): """ extract the numeric value from a string (the string should contain only one numeric value) return the numeric part of the string or None """ s = "" for c in data_str: if c in '1234567890.-': s += c if s: return s else: return None def extract_between(text, sub1, sub2): """ extract a substring from text between first occurances of substrings sub1 and sub2 """ return text.split(sub1, 1)[-1].split(sub2, 1)[0] # test file fname = "violate.dat" mylist = [] flag = False for line in open(fname): if flag == True: mylist.append(line) flag = False if "FCITC" in line: flag = True newlist = [] for item in mylist: temp1 = item[:16] #print(temp1) # test temp1 = extract_number(temp1) temp2 = item[35:83] #print(temp2) # test temp2 = temp2[6:28].replace(']', ' ') temp2 = temp2.strip() #print(temp2) # test temp3 = item[96:144] #print(temp3) # test temp3 = extract_between(temp3, '[B]', '[/B]').rstrip() #print(temp3) # test newlist.append((temp1, temp2, temp3)) print('-'*40) for tup in newlist: print(tup) """my show --> ('690.4', '5567 IND RIV 115', 'Base Case') ('-756.5', '6106 I-STATE 230', '7890 OSCEOLA 230 9190 AGNES-RX') ('589.3', '5704 TAFT 230', ' 2883 INTERCSN 230 5352 CAN ISL') ('1905.9', '6101 MCINTOSH 230', ' 9100 RECKER 230 9150 LKAGNES') ('1119.9', '2167 WINDERME 230', '5353-5800') ('-1121.7', '467 POINSETT 230', '351 ORANGE R 230 354 ORANGE R') ('1776.3', '461 CAPE K 230', '461 CAPE K 230 5703 IND RIV') ('899.9', '5704 TAFT 230', ' 2883 INTERCSN 230 5352 CAN ISL') ('-1670.7', '6106 I-STATE 230', 'BARCOLA 230 6102 WEST') ('593.8', '5704 TAFT 230', '2883 INTERCSN 230 5352 CAN ISL') """
drink her pretty
•
•
Join Date: Apr 2009
Posts: 30
Reputation:
Solved Threads: 0
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
Thanks again,
Joe
•
•
Join Date: Apr 2009
Posts: 30
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- reading from text files and manipulating the data....how (Visual Basic 4 / 5 / 6)
- manipulate text files (VB.NET)
- Trying to compare the contents of two text files and save the difference (Python)
- Need reference to Random Accessing of Text files (C++)
- Write and Read text files (Java)
- Making arrays from text files (VB.NET)
- appending two java text files (Java)
- lost my notebook for making text files--how do I get it back? (Windows NT / 2000 / XP)
Other Threads in the Python Forum
- Previous Thread: Unsupported Operand Types?
- Next Thread: opening an exe from Program Files directory in windows
| Thread Tools | Search this Thread |
Tag cloud for Python
alarm app beginner cipher cmd code copy cx-freeze data decimals dictionary directory dynamic error examples excel file float font format function generator getvalue gui halp homework http import input ip itunes java keycontrol leftmouse line linux list lists logging loop maintain maze millimeter module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode update url urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia windows wxpython






