I have two text files. File_1 has huge data in 100 columns(formatted) and file_2 has mistakes in data entry, has 20 columns and first 15 are same format as data and last 5 contains text like "ACCNT","ADDSS" etc.
file_1 format is as follows
15382987019547317994113................(100 columns)
file_2 format is as follows
153829870195473ACCNT
What i have tried is
program rem_err
implicit none
character * 20 record
character * 15 match, mat_dat
character *5 element,replace
character *80 data1
integer::i
open (9,status='old',file='file_1.txt'
open (10,status='old',file='file_2.txt'
open (11,status='new',file='out.txt'
read (10,1,end=15)record
record(1:15)=match
record(16:20)=element
do 4 i=1,100000
read(9,5,end=15) mat_dat,replace,data1
if(mat_dat.eq.match)then
if (element=ACCNT)replace='*****)
if (element=ADDSS)replace=' '
write(11,5)mat_dat,replace,data1
else
write(11,5)mat_dat,replace,data1
endif
4 continue
goto 2
1 format(a20)
5 format(a15,a5,a80)
15 stop
end program rem_err
How to correct file_1 using information from file_2?
Related
I'm trying to read a file in the format specified below using FORTRAN 66.
1000
MS 1 - Join Grps Group Project 5 5
Four Programs Programming 15 9
Quiz 1 Quizzes 10 7
FORTRAN Programming 25 18
Quiz 2 Quizzes 10 9
HW 1 - Looplang Homework 20 15
I execute and read the file like so:
program < grades.txt
The first line is the total number of points that can be earned in a class
The rest of the lines are assignments in a class
Each line is formatted as such: Assignment name(20 chars) category (20 chars) possible points(14 chars) earned points(14 chars)
For some reason, when the code runs and reads the file, starting at the first assignment record, I get error 5006, and cannot find an explanation of the error code. The output of the program while debugging looks like this:
$ file < grades.txt
MS 1 - Join Grps Group Project 5 6417876
NOT EOF
EOF 5006
NAME CATEGORY POSSIBLE EARNED
My goal is to be able to read each line and put each column into it's appropriate array, then reference those arrays later on to print a report for each category, with each assignment, points possible, earned, and total percentage for the category, then loop, etc.
I do not understand where the "6417876" in the output is coming from, it is definitely not part of the file that's being piped into stdin while the program reads.
The code for the program is as follows:
CHARACTER*20 ASSIGNMENTT(100)
CHARACTER*20 CATEGORY(100)
INTEGER POSSIBLE(100)
INTEGER EARNED(100)
INTEGER TOTALPTS
INTEGER REASON
INTEGER I, N
READ(5,50)TOTALPTS
50 FORMAT(I4)
c Read the arrays in
I=1
100 READ(5,110,IOSTAT=REASON)ASSIGNMENTT(I),CATEGORY(I),POSSIBLE(I),EARNED(I)
110 FORMAT(2A20x,2I14x)
WRITE(*,110)ASSIGNMENTT(I),CATEGORY(I),POSSIBLE(I),EARNED(I)
I=I+1
IF (REASON < 0) GOTO 120
WRITE(*,*)"NOT EOF"
IF (I<100 .AND. REASON == 0) GOTO 100
WRITE(*,*)"EOF", REASON
c Get the number of items (For some reason stdin adds an extra item that's not in the file, so I subtract 2 instead of 1
120 N=I-2
c Display the Names and Ages
WRITE(*,200)
200 FORMAT("NAME",T20,"CATEGORY",T40,"POSSIBLE",T54,"EARNED",T68)
DO 300 I=1,N
210 FORMAT(A20,A20,I14,I14)
300 WRITE(*,210)ASSIGNMENTT(I),CATEGORY(I),POSSIBLE(I),EARNED(I)
END
What could be causing the read issues I'm facing?
The line to read the file contents was too long, so I shortened the names of the variables to save some space and the problem was solved.
I'm currently learning how to write Matrix Arrays to output Text Files in Fortran 95. The problem I'm facing is that, the Matrix Array of 2 Rows by 3 columns I'm working on, is not formatting to what I desire in the Output Text File. I believe, I'm missing One or Two Lines of Codes or failing to add a few codes to the current Lines of Codes I have. Below are My Lines of Codes, Current Output Data and Desired Output Data. The Goal is to get the "Desired Output Data". Kindly show me My mistake(s), what codes/line(s) of codes I'm missing and where I should add the codes/line(s) of codes. Every answer is welcomed and appreciated. Thank you Stackovites.
Lines of Codes:
Program Format2
!illustrates formatting Your Output
Implicit None
Integer:: B(2,3)
Integer:: k,j
!Open Output Text File at Machine 8
Open(8,file="formatoutput2.txt")
Data B/1,3,5,2,4,6/
Do k= 1,2
B(2,3)= k
!Write to File at Machine 8 and show the formatting in Label 11
Write(8,11) B(2,3)
11 format(3i3)
End Do
Do j= 3,6
B(2,3)= j
!Write to File at Machine 8 and show the formatting in Label 12
Write(8,12) B(2,3)
12 format(3i3)
End Do
End Program Format2
Current Output Data
1
2
3
4
5
6
Desired Output Data
1 3 5
2 4 6
B(2,3) refers only to one particular element of the array. Namely the element with index 2 in the first dimension and index 3 in the other dimension. To refer to a different element use B(i,j) where i and j are integers with the desired index. To refer to the whole array use just B or alternatively B(:,:) for an array section that encompasses the whole array.
So to set the values
do j = 1, 3
do i = 1, 2
B(i,j) = i + (j-1) * 2
end do
end do
and to print them use one of the methods showed in countless duplicates (Print a Fortran 2D array as a matrix Write matrix with Fortran How to write the formatted matrix in a lines with fortran77? Output as a matrix in fortran -- search for more, there will be better ones...) on this site
do i = 1, 2
write(8,'(999i3)') B(i,:)
end do
I've seen My mistakes. The instructions I gave the Fortran Compiler, was the result I got in My Output Text File. I was declaring two-2 Rows of (1,2) and (3,4,5,6); instead of declaring Three-3 Columns of (1,2); (3,4) and (5,6). Below is the correct Lines of Codes to get the Desired Output Data.
Lines of Codes:
Program Format2
!illustrates formatting Your Output
Implicit None
Integer:: B(2,3)
Integer:: k,j
!Open Output Text File at Machine 8
Open(8,file="formatoutput2.txt")
Data B/1,2,3,4,5,6/
!(a)Declare The 1st Two-2 Values You want for k Two-2 Rows, that is (1 and 2)
!(b)Note, doing (a), automatically declares the values in Column 1, that is (1 and 2)
Do k= 1,2
B(2,3)= k
End Do
!(c)Next, Declare, the Four Values You want in Column 2 and Column 3. That is [3,4,5,6]
!(d) Based on (c), We want 3 and 4 in "Column 2"; while "Column 3", gets 5 and 6.
Do j= 3,6
B(2,3)= j
End Do
!Write to File at Machine 8 and show the formatting in Label 13
Write(8,13) ((B(k,j),j= 1,3),k= 1,2)
13 format(3i3)
End Program Format2
The Above Lines of Codes, gives the Desired Output below:
1 3 5
2 4 6
I have two CSV files as follows:
CSV1:
**ID Name Address Ph**
1 Mr.C dsf 142
2 Ms.N asd 251
4 Mr.V fgg 014
12 Ms.S trw 547
CSV2:
**ID Name Service Day**
1 Mr.C AAA Mon
2 Ms.N AAA Mon
2 Ms.N BBB Tue
2 Ms.N AAA Sat
As you can see very quickly CSV1 file is unique in having only 1 instance of every ID whilst CSV2 has repeats.
I am trying to match two CSV files based on ID and then wherever they match adding to CSV2 file the Address and Ph fields from CSV1. This is then saved as a new output file leaving the two original CSV files intact.
I have written a code but here's what's happening:
Either all the entries from CSV1 get added against the last row of CSV2
Or all the entries from CSV2 get the same address details appended against them
Here's what I have done so far.
import csv
csv1=open('C:\csv1file.csv')
csv2=open('C:\csv2file.csv')
csv1reader=csv.reader(csv1)
csv2reader=csv.reader(csv2)
outputfile=open('C:\mapped.csv', 'wb')
csvwriter=csv.writer(outputfile)
counter=0
header1=csv1reader.next()
header2=csv2reader.next()
csvwriter.writerow(header2+header1[2:4])
for row1 in csv1reader:
for row2 in csv2reader:
if row1[0]==row2[0]:
counter=counter+1
csvwriter.writerow(row2+row1[2:4])
I am running this code in Python 2.7. As you might have guessed the two different results that I am getting are based on the indentation of the csvwriter statement in the above code. I feel I am quite close to the answer and understand the logic but somehow the loop doesn't loop very well.
Can any one of you please assist?
Thanks.
The problem arises because the inner loop only works once. the reason for that is, because csv2reader will be empty after you run the loop once
a way to fix this would be to make a copy of the rows in the second file and use that copy in the loop
csvwriter.writerow(header2+header1[2:4])
csv2copy=[]
for row2 in csv2reader: csv2copy.append(row2)
for row1 in csv1reader:
for row2 in csv2copy:
print row1,row2,counter
if row1[0]==row2[0]:
counter=counter+1
csvwriter.writerow(row2+row1[2:4])
Suppose i have tabular column as below.Now i want to extract the column wise data.I tried extracting data by creating a list.But it is extracting the first row correctly but from second row onwards there is space i.e under CEN/4.Now my code considers zeroth column has 5.0001e-1 form second row,it starts reading from there. How to extract the data correctly coulmn wise.output is scrambled.
0 1 25 CEN/4 -5.000000E-01 -3.607026E+04 -5.747796E+03 -8.912796E+02 -88.3178
5.000000E-01 3.607026E+04 5.747796E+03 8.912796E+02 1.6822
27 -5.000000E-01 -3.641444E+04 -5.783247E+03 -8.912796E+02 -88.3347
5.000000E-01 3.641444E+04 5.783247E+03 8.912796E+02 1.6653
28 -5.000000E-01 -3.641444E+04 -5.712346E+03 -8.912796E+02 -88.3386
5.000000E-01 3.641444E+04 5.712346E+03 8.912796E+02
my code is :
f1=open('newdata1.txt','w')
L = []
for index, line in enumerate(open('Trial_1.txt','r')):
#print index
if index < 0: #skip first 5 lines
continue
else:
line =line.split()
L.append('%s\t%s\t %s\n' %(line[0], line[1],line[2]))
f1.writelines(L)
f1.close()
my output looks like this:
0 1 CEN/4 -5.000000E-01 -5.120107E+04
5.000000E-01 5.120107E+04 1.028093E+04 5.979930E+03 8.1461
i want columnar data as it is in the file.How to do that.I am a bgeinner
its hard to tell from the way the input data is presented in your question, but Im guessing your file is using tabs to separate columns, in any case, consider using python csv module with the relevant delimiter like:
import csv
with open('input.csv') as f_in, open('newdata1', 'w') as f_out:
reader = csv.reader(f_in, delimiter='\t')
writer = csv.writer(f_out, delimiter='\t')
for row in reader:
writer.writerow(row)
see python csv module documentation for further details
1 from sys import argv
2 from os.path import exists
3
4 script, from_file, to_file = argv
5
6 print "Copying from %s to %s" % (from_file, to_file)
7
8 # we could do these two on one line too, how?
9 input = open(from_file)
10 indata = input.read()
11
12 print "The input file is %d bytes long" % len(indata)
13
14 print "Does the output file exist? %r" % exists(to_file)
15 print "Ready, hit RETURN to continue, CTRL-C to abort."
16 raw_input()
17
18 output = open(to_file, 'w')
19 output.write(indata)
20
21 print "Alright, all done."
22
23 output.close()
24 input.close()
Im not sure what the rule difference is between something like line 19 where there is a variable before the period and also within the parenthesis. I'm a beginner and would like to clarify this because I tried to write some code and was confused about this point...
This means you're calling a method of an object. Let's look at line 18:
output = open(to_file, 'w')
This returns a file object and assigns it to the variable output. You can now call methods of a file object (such as output.read() to read the file's contents). Similarly, you can use output.write(...) to write data to the file:
output.write(indata)
The above line means: write the contents of indata to the file object output. You're correct that there are two variables involved in this operation.