This is my code:
Program Input_Output
Implicit none
Integer::i
Real::R_sn,X_sn,G_sn,B_sn
Open(Unit=2,File='Input.txt',Status='Unknown')
Read(2,'(4(1x,En8.2))')R_sn,X_sn,G_sn,B_sn
Close(Unit=2,Status='Keep')
Open(Unit=4,File='Ouput.txt',Status='Unknown')
Write(4,'(4(1x,En8.2))')R_sn,X_sn,G_sn,B_sn
Close(Unit=4,Status='Keep')
End Program Input_Output
The content of file Input.txt is:
2.09E-01 1.17E-01 0.00E-00 1.16E-04
The content of file Output.txt is:
******** ******** 0.00E+00 ********
My intention with this code is to read some numbers from file and afther that I want to write that numbers in other file in form from file which was used for reading.
What is wrong with writing in Output.txt file?
You're looking for the ES data descriptor, which writes output in 'scientific notation`. This means a single leading digit before the decimal.
write(*,"(4(1x,es8.2))" R_sn, X_sn, G_sn, B_sn
!! output: 2.09E-01 1.17E-01 0.00E+00 1.16E-04
On the other hand, for EN, or 'engineering notation', you are writing (up to) 3 leading digits before the decimal. So you would need to expand the output field (suggested by #VladimirF):
write(*,"(4(1x,en10.2))" R_sn, X_sn, G_sn, B_sn
!! output: 209.00E-03 117.00E-03 0.00E+00 116.00E-06
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 want to read text of unknown lenght from a file into a vector like character(len=1) a(3000) but the input file does not have an "end of file marker"
I tried things like:
open(2,file=surnames.txt,status='old')
read(2,15, IOSTAT=ios,END=20,ERR=20)(a(i),i=1,3000)
isize=0
do while (ios == 0)
isize=isize+1
read(2,15, iostat=ios, end=20) a(isize)
end do
15 format(a1)
20 continue
but nothing works
Thanks for your attention
I have this file, testpi.txt, which i'd like to convert into a list of sentences.
>>>cat testpi.txt
This is math π.
That is moth pie.
Here's what I've done:
r = open('testpi.txt', 'r')
sentence_List = r.readlines()
print sentence_List
And, when the output is sent to another text file - output.txt , this is how it looks like in output.txt:
['This is math \xcf\x80. That is moth pie.\n']
I tried codecs too, r = codecs.open('testpi.txt', 'r',encoding='utf-8'),
but the output then consists of a leading 'u' in all the entries.
How could I display this byte string - \xcf\x80 as π, in the output.txt
Please guide me, thanks.
The problem is you're printing the entire list which gives you an output format you don't want. Instead, print each string individually and it will work:
r = open('t.txt', 'r')
sentence_List = r.readlines()
for line in sentence_List:
print line,
Or:
print "['{}']".format("', '".join(map(str.rstrip, sentence_List)))
I am just learning how to code in Python and have not been able to find a solution or answer as to why when I attempt to read a file that has just been written to it bears additional characters.
Code
#-*-coding:utf-8-*-
from sys import argv
from os.path import exists
script, source, copy = argv
print "We'll be opening, reading, writing to and closing a file"
opensource = open(source)
readsource = opensource.read()
print readsource
print "Great. We opened and read file"
opencopy = open(copy, 'w+') #we want to write and read file
opencopy.write(readsource) #copy the contents of the source file
opencopy.read()
opensource.close()
opencopy.close()
Output
Contents
test °D ΃ ø U ø U ` 6 ` 6 0M Ð
I am running version 2.7 of Python on Windows 7 Professional 64bit.
This seems to be a Windows issue with reading a file opened with "w+" directly after a write.
Start with adding two print statements like this:
opencopy.write(readsource) #copy the contents of the source file
print opencopy.tell()
opencopy.read()
print opencopy.tell()
And run this on a file with only as contents the words 'test' + CR + LF, you get as output:
We'll be opening, reading, writing to and closing a file
test
Great. We opened and read file
6
4098
(If you do the same under Linux, the read does not work beyond the end of the file (and you get two times the value 6 from opencopy.tell().)
What you probably want to do is:
print opencopy.tell()
opencopy.seek(0)
print opencopy.tell()
opencopy.read()
print opencopy.tell()
You then get 6 and 6 as output from tell(). Now this results in reading the word 'test', that
you just wrote.
If you do not want to read what you just wrote, put opencopy.flush() between the read and the write statement:
opencopy.write(readsource) #copy the contents of the source file
print opencopy.tell()
opencopy.flush()
opencopy.read()
print opencopy.tell()
I'm working with large CSV. How can I take a random sample of rows—say, 200 total—and recombine them into a CSV with the same structure as the original?
The procedure I would use is as follows:
Generate 200 unique numbers between 0 and the number of lines in the CSV file.
Read each line of the CSV file and keep a track of which line number your are reading. If its line number matches one of the numbers above, then output it.
Use the Resevoir Sampling random sampling technique that does not require all records be in memory or the actual number of records be known. With it, you stream in you records one-by-one and probabilistically select them into the sample. Once the stream is exhausted, output the final sample records. The technique guarantees each record in the stream has the same probability of being in the final sample. That is to say, it generates a simple random sample.
You can use random module's random.sample method to randomize a list of line offsets as shown below.
import random
# Fetching line offsets.
# Courtesy: Adam Rosenfield's tip about how to read a HUGE text file.
# http://stackoverflow.com/questions/620367/
# Read in the file once and build a list of line offsets
line_offset = []
offset = 0
for line in file:
line_offset.append(offset)
offset += len(line)
file.seek(0)
# Part where you pick the random lines and copy to your new file
# My 2 cents.
randoffsets = random.sample(line_offset, 200)
with open('your_file') as f:
for k in randoffsets:
f.seek(k)
f.readline() # and append to your new file
You could try to use linecache if it works for you but since linecache reads the entire file into memory I'm not sure how well it would work for a 6GB file.