The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Query for a FOR loop
Discuss Query for a FOR loop in the Python Programming forum on Dev Shed. Query for a FOR loop Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 2nd, 2013, 05:41 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 14
Time spent in forums: 2 h 46 m 47 sec
Reputation Power: 0
|
|
Query for a FOR loop
Hi Guys
I have a text file with the following content
ENOVIpdIntegration\DataNav.m\LocalInterfaces\ENOVIDFHandler.h has been modified
ENOVIpdIntegration\DataNav.m\src\ENOVExportFile.cpp has been modified
ENOVIpdIntegration\DataNav.m\src\ENOVIDFHandler.cpp has been modified
ENOVIpdIntegration\DataNav.m\src\ENOVIpdUtilities.cpp has been modified
ENOVIpdIntegration\DataNav.m\src\ExportEV5Info.cpp has been modified
ENOVIpdIntegration\PrivateInterfaces\ENOVExportFile.h has been modified
ENOVIpdIntegration\PrivateInterfaces\ENOVIpdUtilities.h has been modified
ENOVIpdIntegration\PrivateInterfaces\ExportEV5Info.h has been modified
DNBDpmStrCommonUI\CNext\VBScript\Structure_Manufacturing has been created
DNBDpmStrCommonUI\CNext\VBScript\Structure_Manufacturing\FEUILLET.catvba has been created
DNBDpmStrCommonUI\CNext\VBScript\Structure_Manufacturing\Ressources has been created
DNBDpmStrCommonUI\CNext\VBScript\Structure_Manufacturing\Ressources\Built_In has been created
DNBDpmStrCommonUI\CNext\VBScript\Structure_Manufacturing\Ressources\Built_In\BUILT-IN_Table_PREPA.xls has been created
DNBDpmStrCommonUI\CNext\VBScript\Structure_Manufacturing\Ressources\Rapport has been created
DNBDpmStrCommonUI\CNext\VBScript\Structure_Manufacturing\Ressources\Rapport\Design_Unit_043.csv has been created
DNBDpmStrCommonUI\CNext\VBScript\Structure_Manufacturing\Ressources\Template_V5 has been created
DNBDpmStrCommonUI\CNext\VBScript\Structure_Manufacturing\Ressources\Template_V5\DATA_FEUILLET has been created
I have the folowing code
def fn_num_lines(fname):
num_lines = 0
with open(fname, 'r') as f:
for line in f:
words = line.split()
num_lines += 1
return num_lines
def fn_some(fname):
num_lines=fn_num_lines(fname)
f=open(fname)
read_log=f.readlines()
for line in read_log:
for i in range(num_lines):
str=line.split( );
#print str
f2=open("f2.txt", "a")
f2.write(str[0]+"\n")
f2.close
break
f.close
fn_some1(num_lines)
def fn_some1(num_lines):
file1="f2.txt"
print num_lines
f1=open(file1)
read_log1=f1.readlines()
for line1 in read_log1:
for j in range(num_lines):
str1=line1.split("\\")[-1];
#print str1
f3=open("f1.txt", "a")
f3.write(str1)
f3.close
break
f1.close
fn_some("test.txt")
num_lines = fn_num_lines("test.txt")
The code works fine, unfortunately during the function call of def fn_some1(num_lines), if i have say num_lines=17 it only printing 16 lines and leaving out the last line.
Any help would be great as I'm totally confused as what is the problem.
Thanks
|

January 2nd, 2013, 10:53 AM
|
 |
Contributing User
|
|
Join Date: May 2012
Location: 39N 104.28W
Posts: 90
Time spent in forums: 1 Day 13 h 37 m 44 sec
Reputation Power: 2
|
|
|
Without code tags it's impossible to determine if you've made an indentation mistake (which could easily account for your problem).
In fn_some1(), you pass in the num_lines from "fname". However, you open "f2.txt". Programmatically, it's possible (and therefore likely vis: Fermat) that those wouldn't be the same file.
Then, for each element in read_log1, which is the same as each line in f2.txt, you range over the number of lines. That seems like bad logic. Again, without code tags I can't say what you're really doing there.
|

January 2nd, 2013, 09:18 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 14
Time spent in forums: 2 h 46 m 47 sec
Reputation Power: 0
|
|
|
Hi
Well what I'm trying to do is I have a text file named test.txt which has the following content
ENOVIpdIntegration\DataNav.m\LocalInterfaces\ENOVIDFHandler.h has been modified
ENOVIpdIntegration\DataNav.m\src\ENOVExportFile.cpp has been modified
ENOVIpdIntegration\DataNav.m\src\ENOVIDFHandler.cpp has been modified
ENOVIpdIntegration\DataNav.m\src\ENOVIpdUtilities.cpp has been modified
ENOVIpdIntegration\DataNav.m\src\ExportEV5Info.cpp has been modified
ENOVIpdIntegration\PrivateInterfaces\ENOVExportFile.h has been modified
Now I call the function fn_some("test.txt") which calls the function fn_num_lines("test.txt") to get the number of lines in the file which is 6
Then I am splitting the contents of test.txt line by line in order to retrieve the output of each line in 3 different variables (retrieving just one variable at the moment)
1. path (example would be ENOVIpdIntegration\DataNav.m\LocalInterfaces\)
2. filename (example would be ENOVIDFHandler.h)
3. keyword (modified)
As of the code above I'm just retrieving the path alone and storing it in another text file called f2.txt.
So the contents of the file f2.txt would be
ENOVIpdIntegration\DataNav.m\LocalInterfaces\ENOVIDFHandler.h
ENOVIpdIntegration\DataNav.m\src\ENOVExportFile.cpp
ENOVIpdIntegration\DataNav.m\src\ENOVIDFHandler.cpp
ENOVIpdIntegration\DataNav.m\src\ENOVIpdUtilities.cpp
ENOVIpdIntegration\DataNav.m\src\ExportEV5Info.cpp
ENOVIpdIntegration\PrivateInterfaces\ENOVExportFile.h
Once that is done i call the function fn_some1(num_lines), which is supposed to split the contents of the file f2.txt line by line and provide the O/p as follows
ENOVIDFHandler.h
ENOVExportFile.cpp
ENOVIDFHandler.cpp
ENOVIpdUtilities.cpp
ExportEV5Info.cpp
ENOVExportFile.h
But the problem is that i get the O/P only for 5 lines and it ignores the 6th line.
Hope this helps in providing me a solution
Thanks for the reply
|

January 3rd, 2013, 08:35 AM
|
 |
Contributing User
|
|
|
|
Code:
data = r'''ENOVIpdIntegration\DataNav.m\LocalInterfaces\ENOVIDFHandler.h has been modified
ENOVIpdIntegration\DataNav.m\src\ENOVExportFile.cpp has been modified
ENOVIpdIntegration\DataNav.m\src\ENOVIDFHandler.cpp has been modified
ENOVIpdIntegration\DataNav.m\src\ENOVIpdUtilities.cpp has been modified
ENOVIpdIntegration\DataNav.m\src\ExportEV5Info.cpp has been modified
ENOVIpdIntegration\PrivateInterfaces\ENOVExportFile.h has been modified
'''
import os
import pprint
def line_count(A):
return A.count('\n') + ('\n' != data[-1])
def process_line(A):
FIELDS = A.split(' ')
T = os.path.split(FIELDS[0])
return T + (FIELDS[-1],)
def group(A):
result = ([],[],[],)
for L in A.split('\n'):
for (B,C,) in enumerate(process_line(L)):
result[B].append(C)
return result
pprint.pprint(group(data)) #which I'm sure will work on your DOS system.
print('\n\nthere are %d lines'%line_count(data))
__________________
[code] Code tags[/code] are essential for python code!
|

January 3rd, 2013, 09:37 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 12
Time spent in forums: 3 h 51 m 27 sec
Reputation Power: 0
|
|
The above code works, but if you want something that's a little bit closer to your original code, then try this:
Code:
def fn_num_lines(fname):
num_lines = 0
with open(fname, 'r') as f:
for line in f:
words = line.split()
num_lines += 1
return num_lines
def fn_some(fname):
names = []
num_lines=fn_num_lines(fname)
f=open(fname)
read_log=f.readlines()
for line in read_log:
for i in range(num_lines):
str3=line.split( )
print str3
#print str
f2=open("f2.txt", "a")
names.append(str3[0])
f2.write(str3[0] + '\n')
f2.close
break
f.close
fn_some1(num_lines, names)
def fn_some1(num_lines, names):
file1="f2.txt"
print num_lines
f1=open(file1, 'r')
for y in names:
for j in range(num_lines):
z = str(y)
str1=z.split("\\")[-1]
f3=open("f1.txt", "a")
f3.write(str1 + '\n')
f3.close
break
f1.close
fn_some("test.txt")
num_lines = fn_num_lines("test.txt")
The main difference is that I stored everything in a list, which I transferred from fn_some to fn_some1 (note the added function argument). Readlines() can cause problems, so if you find that you're having trouble with it, then sometimes it's best to find an alternate method.
Some things can now be removed from the above code, but I'll let you decide if you want to do that or not.
I also changed one of your variable names. You had
Code:
str = line.split( )
This is not a good idea. Always avoid using str, open, list, int, float, and similar things as variable names.
|

January 3rd, 2013, 09:56 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 14
Time spent in forums: 2 h 46 m 47 sec
Reputation Power: 0
|
|
|
Thanks a lot guys it works perfectly fine.
@StevenJM: Why do you say that readlines() function could cause problems.
And do you know of any other way to go about this.
Also, I have another piece of code which is giving me the run.
def fn_ret_det(f1, f2, num_lines):
#print f1 + " " + f2
f11=open(f1)
f22=open(f2)
read_log1=f11.readlines()
read_log2=f22.readlines()
i=0
for line1 in read_log1:
line1=line1.strip( )
for line2 in read_log2:
f4=open("user_details.bat", "a")
line2=line2.strip( )
if(line2=="modified"):
line2="content"
elif(line2=="created"):
line2="del"
a="call adl_ds_chg" + " " + line1 + " -chg" + " " + line2 + " -program -sep \"|\">zlog\\log_" + str(i) +".txt\n"
f4.write(a)
f4.close
break
i=i+1
f11.close
f22.close
os.system("call user_details.bat" + " >log.txt")
return 0
funct_ret=fn_ret_det(file1, file2, num_lines)
The contents of the file f2 and f3 are as follows (respct)
F2.txt
DELE5Client\bombrowser.m\src\stdafx.cpp
DELE5Server\eplogger.m\src\eplogger.cpp
F3.txt
modified
modified
So the problem is: in the O/p of the log.txt file (as shown below) I'm getting only 1 line of o/p with a "/n" as the first line
</n> (an extra line with no text)
D:\chd_wzwr23tstws>call adl_ds_chg DELE5Client\bombrowser.m\src\stdafx.cpp -chg content -program -sep "|" 1>zlog\log_0.txt
I'm not sure what i'm doing wrong over here.
please do help
Thanks a lot
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|