i have a text file which contain several lines in the end of the file I have the following line: "Total: 235267878"
my question is: How do I retrieve the specific value (235267878) and set it to a variable?
Thanks!
Since you didn't specify how long your file can be, we can iterate through the file:
with open('test.txt', 'r') as file:
for line in file:
if 'Total:' in line:
totalValue = line.split(':')[-1].strip()
print(totalValue)
In this solution I assume that the line we are looking for always has the form Total: {number}. We open the file in the read only mode and iterate through the lines (In my exmaple the file is called test.txt). After the line containing the total value is found, we split it and remove possible white spaces to get the number. The variable totalValue contains the number you are looking for.
Related
Is there a way to get a specific line inside a text file without iterating line-by-line in C?
for example I have this text file names.txt it contains the following names below;
John
James
Julia
Jasmine
and I want to access 'Julia' right away without iterating through 'John' and 'James'?, Something like, just give the index value of '2' or '3' to access 'Julia' right away.
Is there a way to do this in C?
I just want to know how because I want to deal with a very large text file something like in about 3 billion lines and I want to access a specific line in there right away and iterating line-by-line is very slow
You have to at least once iterate thru all lines. In this iteration, before reading a line, you record the position in the file and save it to an array or to another file (Usually named an index file). The file shall have a fixed record size that is good for storing the position off the line in the text file.
Later, when you want to access a give line, you either use the array to get the position (Line number is the array index) or the file (You seek into the file to offset line number of record size) and read the position. Once you get the position, you can see into the text file to that position and read the line.
Each time the text file is updated, you must reconstruct the array or index file.
There are other way to do that, but you need to better explain the context.
I have a text file with multiple of lines. Each line contains a known word and an unknown number. I want to make a batch file which searches for a specific line (with a known word) and replaces the random number with a number with is entered by the user.
I already used FART but I don't know the part with the unknown number.
I am trying to take an input from a text file in this format:
Processed_kplr010074716-2009131105131_llc.fits.txt
Processed_kplr010074716-2009166043257_llc.fits.txt
Processed_kplr010074716-2009259160929_llc.fits.txt
etc.... (there are several hundred lines)
and use that input to name my output files for a Matlab loop. Each time the loop ends, i would like it to process the results and save them to a file such as:
Matlab_Processed_kplr010074716-2009131105131_llc.fits.txt
This would make identifying the object which has been processed easier as I can then just look for the ID number and not of to sort through a list of random saved filenames. I also need it to save plots that are generated in each loop in a similar fashion.
This is what I have so far:
fileNames = fopen('file_list_1.txt', 'rt');
inText = textscan(fileNames, '%s');
outText = [inText]';
fclose(fileNames)
for j:numel(Data)
%Do Stuff
save(strcat('Matlab_',outText(j),'.txt'))
print(Plot, '-djpeg', strcat(outText(j),'.txt'))
end
Any help is appreciated, thanks.
If you want to use the save command to save to a text file, you need to use -ascii tab, see the documentation for more details. You might also want to use dlmwrite instead(or even fprintf, but I don't believe you can write the whole matrix at once with fprintf, you have to loop over the rows).
My goal is to print out every full line from a text file if that line contains a string that is equivalent to user input.
I understand how to find the occurrences of a specific string in a text file, but I am confused as to how to associate that with a specific line. How do I relate my string with the specific line that it is in?
My initial thought was to store each line in an array and then print out that line if the user string is somewhere in that line.
However each line is a different size, so I was wondering if it is possible for me to initially divide my entire text file into x number of lines and then use a loop to go through each line and search for that string?
Save the file pointer of the starting of the line in a temp variable before starting new line compare
I would like to read a file, find some strings and replace everything that is after the symbol "=" in this line.
Lets say I have a textfile like this:
name=whatever
age=150
id.from.system=10298092_42_42
path=D:\name\somewhere
whatever_A= WHATEVER
Lets say I want to change path. At first I have to find the string "path" and then replace everything after "=" somehow. Any ideas? I know I could easily read the file line by line something like this:
val source = io.Source.fromFile("C:/myfile.txt)
val lines = source.mkString
source.close()
But this is maybe not the best idea, because its not that performant to read the whole file (maybe the file got 10000000 lines, and the string is already at line 2, but my program would read the whole file. That would be unnecessary).
And there is maybe another problem: if Im searching for specific strings, like here for "name" but these strings are there several times. I want to make sure that its only valid is after the string there is an "=". Maybe I could search always for something with an "=" at the end, that could solve the problem. But I have no idea how to write this in a nice scala code.
You can use an iterator to only iterate until you find the line you're looking for.
val source = io.Source.fromFile("somePath").getLines
val line = source.find(_.startsWith("path="))
line will contain the first line that starts with "path=".
If your C:/myfile.txt contains the line path=D:\name\somewhere, you can replace D:\name\somewhere with the following code:
val lines = fromString("path=D:\\name\\somewhere").getLines // use fromFile here
for { in <- lines
out <- if (in startsWith("path=")) "path=D:\\my\\path" else in
} yield out
This example will return the string
path=D:\my\path
You would need to use fromFile to get the lines and write the lines out to a new file.
Here's another approach that accomplishes the same thing:
val lines = fromString("path=D:\\name\\somewhere").getLines
lines.map(in => if (in startsWith("path=")) "path=D:\\my\\path" else in)