I am new to python and this may be a foolish question but it troubles me for several days.
I have about 30 log files, and each of them contains strings and data. They are almost the same except the difference of several data, and their names are arranged regularly, like ‘log10.lammps’, ‘log20.lammps’, etc.(The ’10”20’represent the temperature of the simulation). I want to write a python script, which loops all these files and read their data in a specific line, (say line3900). Then I want to write these data to another data file, which is arranged like these:
10 XXX
20 XXX
30 XXX
.
.
.
I can read and write from a single file, but I cannot achieve the loop. Could anyone please tell me how to to that. Thanks a lot!
PS. Still another difficulty is that the data in line 3900 is presented like this: “The C11 is 180.1265465616”, the one I want to extract is 180.1265465616. How can I extract the number without the strings?
This answer covers how to get all the files in a folder in Python. To summarize the top answer, to get all the files in a single folder, you would do this:
The next step would be to extract the number from the line
The C11 is 180.1265465616.I’m assuming that you have a function called
get_linethat given a filename, will return that exact line.You can do one of three things. If the length of the number at the end is constant, then you can just grab the last n characters in the string and convert it to a number. Alternatively, you could split the string by the spaces and grab the last item — the number. Finally, you could use Regex.
I’m just going to go with the second option since it looks the most straightforward for now.
I wasn’t sure how you wanted to write the numbers to the file, but hopefully these should help you get started.