Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert_to_csv.py does not work under windwos due to ":" in the csv paths #39

Open
mr15 opened this issue Dec 24, 2024 · 1 comment
Open

Comments

@mr15
Copy link

mr15 commented Dec 24, 2024

When running this script under Windows, I get the following error:

D:\Dateien\Dokumente\pyEmonCmsDownload>python.exe -m convert_to_csv.py
Converting phpfina feed 104076
Traceback (most recent call last):
  File "<frozen runpy>", line 189, in _run_module_as_main
  File "<frozen runpy>", line 112, in _get_module_details
  File "D:\Dateien\Dokumente\pyEmonCmsDownload\convert_to_csv.py", line 57, in <module>
    phpfina_convert(username+"/phpfina/",username+"/csv/",f['id'],f['tag'],f['name'])
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Dateien\Dokumente\pyEmonCmsDownload\convert_to_csv.py", line 22, in phpfina_convert
    csv_fh = open(csvdir+str(feedid)+"-"+str(tag)+"-"+str(name)+".csv","w")
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: [Errno 22] Invalid argument: 'niclas/csv/104076-Node:0-node:0:Z1_Temp01-T_DHW_oben.csv'

I think this is due to windows not allowing ":" in file names.

My proposal for a fix: Replace ":" with "-". Here is what this could look like (worked for me), essentially adding those two lines:

csv_path = csvdir+str(feedid)+"-"+str(tag)+"-"+str(name)+".csv"
csv_path = csv_path.replace(":", "-")

Resulting in:

def phpfina_convert(datadir,csvdir,feedid,tag,name):
    meta = phpfina_get_meta(datadir,feedid)
    print(tag)
    print(name)
    csv_path = csvdir+str(feedid)+"-"+str(tag)+"-"+str(name)+".csv"
    csv_path= csv_path.replace(":", "-")
    csv_fh = open(csv_path,"w")
    fh = open(datadir+str(feedid)+".dat","rb")
    for i in range(0,meta['npoints']):
        time = meta['start_time'] + i*meta['interval']
        val = struct.unpack("f",fh.read(4))
        csv_fh.write(str(time)+","+str(val[0])+"\n")
    fh.close()
    csv_fh.close()
    
def phptimeseries_convert(datadir,csvdir,feedid,tag,name):
    if os.path.isfile(datadir+"feed_"+str(feedid)+".MYD"):
        bytesize = os.stat(datadir+"feed_"+str(feedid)+".MYD").st_size
        npoints = int(bytesize/9.0)
        csv_path = csvdir+str(feedid)+"-"+str(tag)+"-"+str(name)+".csv"
        csv_path = csv_path.replace(":", "-")
        csv_fh = open(csv_path,"w")
        fh = open(datadir+"feed_"+str(feedid)+".MYD","rb")
        
        for i in range(0,npoints):
            tmp = struct.unpack('<xIf',fh.read(9))
            csv_fh.write(str(tmp[0])+","+str(tmp[1])+"\n")
        fh.close()
        csv_fh.close()
@mr15
Copy link
Author

mr15 commented Dec 24, 2024

I created PR #40 for this proposed fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant