Saving (unusual) linux filenames

A

AmFreak

Hi,

i have a script that reads and writes linux paths in a file. I save the
path (as unicode) with 2 other variables. I save them seperated by "," and
the "packets" by newlines. So my file looks like this:
path1, var1A, var1B
path2, var2A, var2B
path3, var3A, var3B
.....

this works for "normal" paths but as soon as i have a path that does
include a "," it breaks. The problem now is that (afaik) linux allows
every char (aside from "/" and null) to be used in filenames. The only
solution i can think of is using null as a seperator, but there have to a
cleaner version ?

Thanks for any help

Biene_Maja
 
G

Grant Edwards

Hi,

i have a script that reads and writes linux paths in a file. I save the
path (as unicode) with 2 other variables. I save them seperated by "," and
the "packets" by newlines. So my file looks like this:
path1, var1A, var1B
path2, var2A, var2B
path3, var3A, var3B
....

this works for "normal" paths but as soon as i have a path that does
include a "," it breaks. The problem now is that (afaik) linux allows
every char (aside from "/" and null) to be used in filenames. The only
solution i can think of is using null as a seperator, but there have to a
cleaner version ?

The normal thing to do is to escape the delimiter when it appears in
data. There are lots of plenty of escaping standards to choose from,
and some of them (e.g. the one used for URLs) are already present
in various bits of Python's standard library.
 
A

Arnaud Delobelle

Hi,

i have a script that reads and writes linux paths in a file. I save
the path (as unicode) with 2 other variables. I save them seperated by
"," and the "packets" by newlines. So my file looks like this:
path1, var1A, var1B
path2, var2A, var2B
path3, var3A, var3B
....

this works for "normal" paths but as soon as i have a path that does
include a "," it breaks. The problem now is that (afaik) linux allows
every char (aside from "/" and null) to be used in filenames. The only
solution i can think of is using null as a seperator, but there have
to a cleaner version ?

Thanks for any help

Biene_Maja

A simple solution would be to save each line of data using JSON with the json
module:
import json


path = "x,y,z"
varA = 12
varB = "abc"
line = json.dumps([path, varA, varB])
print line ["x,y,z", 12, "abc"]
loadpathA, loadvarA, loadvarB = json.loads(line)
print loadpathA x,y,z
print loadvarA 12
print loadvarB
abc

HTH
 
A

AmFreak

Thanks for all the nice answers!
The normal thing to do is to escape the delimiter when it appears in
data. There are lots of plenty of escaping standards to choose from,
and some of them (e.g. the one used for URLs) are already present
in various bits of Python's standard library.

The CSV module has something like that, but im using unicode and it
doesn't work with that.


Why is your impression that the null character is "dirty"?
E.g. that's how find|xargs etc. usually work.
Another alternative would be if you gaurantee that your varn's don't
have commas then put the path last. But that doesn't account for
filenames containing newlines.
Another alternative would be to wrap with some kind of serialization
library. But really, what's so dirty about null?

I think i just prefer a little formated file instead of one loooong row :)



A simple solution would be to save each line of data using JSON with the
json
module:
import json
path = "x,y,z"
varA = 12
varB = "abc"
line = json.dumps([path, varA, varB])
print line ["x,y,z", 12, "abc"]
loadpathA, loadvarA, loadvarB = json.loads(line)
print loadpathA x,y,z
print loadvarA 12
print loadvarB
abc

Thanks, just tried it - so simple, but seems to work like a charm. Really
aprecciated :D.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top