How to read .mod files using Python - python

I have a file with extension .mod.
That file contains fields and data under each field, just like every csv files do.
I need to read this .mod file using Python.
Please suggest me a way out in Python using Pandas or any other package that could help me with this.
Thanks!

On Windows 10, using Python 3.6, I could successfully open the file and read the first line:
with open('09nested.mod') as f:
print(f.readlines()[0])
// File: 09nested.mod
>>>

Related

Read xlsx file as dataframe inside .rar pack in python directly

I need help to read xlsx file present inside rar pack. I am using below code, however get an error. Is there any better way to read/extract file?
rar = glob.glob(INPATH + "*xyz*.rar*")
rf = rarfile.RarFile(rar[0])
for f in rf.infolist():
print(f.filename, f.file_size)
df = pd.read_excel(rf.read(f))
rarfile.RarCannotExec: Cannot find working tool
According to the brief PyPI docs, you need unrar installed and on your PATH in order for the module to work. It does not implement the RAR unpacking algorithm itself.
(Presumably you need rar as well, for creating archives.)

Read paths in csv and open in unix

I need to read paths in a csv and go to these file paths in unix. I am wondering if there is any way to do that using unix or python commands. I am unsure of how to proceed and I am not able to find much resources in the net either.
The number of rows in the excel.csv is a lot and similar to below. I need to open the excel.csv and then read the first line and go to this file path. Once this file is opened using the file path, I need to be able to read the file and extract out certain information. I tried using python for this but I am unable to find much information and I am wondering if I can use unix commands to solve this. I am clueless on how to proceed for this one so I would appreciate any reference or help using either python or unix commands. Thank you!
/folder/file1
/folder/file2
/folder/file3
It shouldn't be very difficult to do this in Python, as reading csv files is part of the standard library. Your code could look something like this:
with open('data.csv', newline='') as fh:
# depending if the first row describes the header or not,
# you can also use the simple csv.reader here
for row in csv.DictReader(fh, strict=True):
# again, if you use the simple csv.reader, you'll have to access
# the column via index instead
file_path = row['path']
with open(file_path, 'r') as fh2:
data = fh2.read()
# do something with data

Is it possible to change save path of a file saved by an external library?

I use a library in python called pyansys in which I use a method called save_as_vtk.
There it is: documentation
This method generates a file for me and saves it to my working directory. I would like that file to be saved elsewhere... I don't want it moved because sometimes it is 20+ Gb and it would take too long.
Anybody has an idea?
Thank you!
I'm the maintainer of the pyansys package.
This was answered in https://github.com/akaszynski/pyansys/issues/219
Repeated here:
It appears that ResultFile.save_as_vtk already has a filename parameter:
def save_as_vtk(self, filename, rsets=None, result_types=['ENS']):
"""Writes results to a vtk readable file.
The file extension will select the type of writer to use.
``'.vtk'`` will use the legacy writer, while ``'.vtu'`` will
select the VTK XML writer.
Parameters
----------
filename : str
Filename of grid to be written. The file extension will
select the type of writer to use. ``'.vtk'`` will use the
legacy writer, while ``'.vtu'`` will select the VTK XML
writer.

How do I create an empty csv file on a specific folder?

I had a doubt on how to create an empty csv file where I can open it later to save some data using python. How do I do it?
Thanks
An empty csv file can be created like any other file - you just have to perform any operation on it. With bash, you can do touch /path/to/my/file.csv.
Note that you do not have to create an empty file for python to write in. Python will do so automatically if you write to a non-existing file. In fact, creating an empty file from within python means to open it for writing, but not writing anything to it.
with open("foo.csv", "w") as my_empty_csv:
# now you have an empty file already
pass # or write something to it already
you can also use Pandas to do the same as below:
import pandas as pd
df = pd.DataFrame(list())
df.to_csv('empty_csv.csv')
After creating above file you can Edit exported file as per your requirement.

Compare archiwum.rar content and extracted data from .rar in the folder on Windows 7

Does anyone know how to compare amount of files and size of the files in archiwum.rar and its extracted content in the folder?
The reason I want to do this, is that server I'am working on has been restarted couple of times during extraction and I am not sure, if all the files has been extracted correctly.
.rar files are more then 100GB's each and server is not that fast.
Any ideas?
ps. if the solution would be some code instead standalone program, my preference is Python.
Thanks
In Python you can use RarFile module. The usage is similar to build-in module ZipFile.
import rarfile
import os.path
extracted_dir_name = "samples/sample" # Directory with extracted files
file = rarfile.RarFile("samples/sample.rar", "r")
# list file information
for info in file.infolist():
print info.filename, info.date_time, info.file_size
# Compare with extracted file here
extracted_file = os.path.join(extracted_dir_name, info.filename)
if info.file_size != os.path.getsize(extracted_file):
print "Different size!"

Categories

Resources