I want to import a binary file within a module through a name.
For example:
mylibrary
|
+---test (name of submodule)
|
+---TestData (name representing the binary file)
|
+---testdata.bin (the actual binary file)
I want:
import numpy as np
import mylibrary.test.TestData as TestData
with open(TestData, 'rb') as f:
np_array = np.load(f)
Is that a good idea? How to make this work?
You cannot do it like that, Python will not recognize non Python files when importing. But you can add __init__.py file to the test folder and place in it:
import os
TestData = os.path.join(os.path.dirname(os.path.realpath(__file__)), "testdata.bin")
Then you can easily load it in your other projects as:
import numpy as np
from mylibrary.test import TestData
with open(TestData, 'rb') as f:
np_array = np.load(f)
Related
I know how to tar file using Python
import os
import tarfile
with tarfile.open('res.tar.gz','w:xz' )as tar :
tar.add('Pic.jpeg')
But I want to do that without create any tar.gz file, only get the results buffer.
Hiw can I do that please?
You could use this code to access result buffer
from io import BytesIO
import os
import tarfile
buf = BytesIO()
with tarfile.open('/tmp/res.tar.gz', 'w:gz', fileobj=buf) as f:
f.add("Pic.jpeg")
data = buf.getvalue()
So an update, I found my compile issue was that I needed to change my notebook to a py file and choosing save as doesn't do that. So I had to run a different script turn my notebook to a py file. And part of my exe issue was I was using the fopen command that apparently isn't useable when compiled into a exe. So I redid the code to what is above. But now I get a write error when trying to run the script. I can not find anything on write functions with os is there somewhere else I should look?
Original code:
import requests
import json
import pandas as pd
import csv
from pathlib import Path
response = requests.get('url', headers={'CERT': 'cert'}, stream=True).json()
json2 = json.dumps(response)
f = open('data.json', 'r+')
f.write(json2)
f.close()
Path altered code:
import requests
import json
import pandas as pd
import csv
from pathlib import Path
response = requests.get('url', headers={'CERT': 'cert'}, stream=True).json()
json2 = json.dumps(response)
filename = 'data.json'
if '_MEIPASS2' in os.environ:
filename = os.path.join(os.environ['_MEIPASS2'], filename)
fd = open(filename, 'r+')
fd.write(json2)
fd.close()
The changes to the code allowed me to get past the fopen issue but created a write issue. Any ideas?
If you want to write to a file, you have to open it as writable.
fd = open(filename, 'wb')
Although I don't know why you're opening it in binary if you're writing text.
i am trying to run a file "reader_microphone.py" with some of its line below
import pyaudio
import numpy
import wave
from reader import basereader
class MicrophoneReader(BaseReader):
default_chunksize = 8192
default_format = pyaudio.paInt16
default_channels = 2
default_rate = 44100
default_seconds = 0
i am getting this error-
from reader import basereader
ImportError: cannot import name 'basereader' from 'reader' (C:\ProgramData\Anaconda3\lib\reader.py)
and welcome to StackOverflow.
There is no basereader reference in the reader package. You can take a look at the package's documentation here, to try and see what's the object you are looking for.
I have to parse config.ini file in another directory and read few values using python.
You can use the ConfigParser module.Check examples here .
import ConfigParser
Config = ConfigParser.ConfigParser()
Config.read("\\somepath\\config.ini")
Config.sections()
I think you should read the configparser documentation.
To simple read a .ini configuration file from a path, this should get you started:
import configparser
# Create config parser
config = configparser.ConfigParser()
# Read in config.ini from a path
config.read('path/to/config.ini')
# Do more parsing.
The above specifically uses configparser.ConfigParser.read() to read in the .ini file.
You would need to provide more detail in your question for more in depth parsing help.
import os
import datetime
import time
import numpy as np
import pandas as pd
yesterday_date = (datetime.datetime.today() - datetime.timedelta(days=1)).strftime('%Y-%m-%d')
for name in os.listdir('D:\Test'):
full_name = os.path.join('D:\Test', name)
if time.strftime('%Y-%m-%d', time.localtime(os.path.getmtime(full_name))) == yesterday_date:
folder_name_path = 'D:\Test\\' + name
filename_path = folder_name_path + '\\' + 'mentionfilename'
from openpyxl.workbook import Workbook
df = pd.read_excel(r'C:\Users\Desktop\testing_today.xlsx')
df_old = pd.read_excel(filenamepath)
df_new=df.merge(df_old[['mtm','wes','fre']],on='mtm')
writer =pd.ExcelWriter(r'C:\Users\Desktop\output.xlsx')
df_new.to_excel(writer,'Sheet1')
writer.save()
I downloaded this dataset and stored it in a folder called AutomobileDataset.
I cross checked the working directory using:
import pandas as pd
import numpy as np
import os
os.chdir("/Users/madan/Desktop/ML/Datasets/AutomobileDataset")
os.getcwd()
Output:
'/Users/madan/Desktop/ML/Datasets/AutomobileDataset'
Then I tried reading the file using pandas:
import pandas as pd
import numpy as np
import os
os.chdir("/Users/madan/Desktop/ML/Datasets/AutomobileDataset")
os.getcwd()
automobile_data = pd.read_csv("AutomobileDataset.txt", sep = ',',
header = None, na_values = '?')
automobile_data.head()
Output:
---------------------------------------------------------------------------
ParserError: Error tokenizing data. C error: Expected 1 fields in line 2, saw 26
Someone please help me with this, I don't know where I am making a mistake.
Can try this!
import os
# Read in a plain text file
with open(os.path.join("c:user/xxx/xx", "xxx.txt"), "r") as f:
text = f.read()
print(text)