[Tutor] Problem Moving Python Program From WIndows to Linux
Jethro Cramp
jsc@rock-tnsc.com
Tue, 10 Apr 2001 16:34:02 -0800
I have hit upon a thorny little problem. I had started building a
program on Windows ME and then I moved over to Linux and want to keep
working on it.
I have hit a number of problems where my python files won't run on
Linux. On Windows there is no problem.
On both systems I am using Python 2.0. My Linux system is SuSE7.1
The problem is to do with reading files and then splitting the fields.
So I have a text file of the following format:
Imperial Black =3.01
Mountain White =2.7
Canton Green =2.8
etc.etc
These are names of different types of stone and their mass in MT.
The format is <NAME> a couple of tabs for legibility "=" then <MASS>
The code I use is like this:
def Get_Materials_List():
materialsfile = open(rkpaths.material_specs, "r")
materials = {}
for material in materialsfile.readlines():
#Ignores blank lines, comments, and currency demarcation
if not material=="\n" and not material[:1] == "#" and not
material[:1] =="[":
#Get the material name and the mass stripping white spaces
print material
mat, mass = map(string.strip, string.split(material, "="))
materials[string.upper(mat)] = (mat,mass)
The error I get when I run this code is:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.0/rk/rkcalc.py", line 172, in Get_Materials_List
mat, mass = map(string.strip, string.split(material, "="))
ValueError: unpack list of wrong size
Are the tab stops being treated differently in Linux from Windows?
Also when I open a file in idle that I wrote under windows there is a
"\r" at the end of each line. Is this some kind of windows specific end
of line character.
If I read the file manually like so:
>>> file = open(rkpaths.material_specs, "r")
>>> x = file.readlines()
for entry in x:
print entry
# This file contains a list of materials and their mass in MT/m3\r
\r
Imperial Black = 3.01\r
Chinese Tarn = 2.7\r
Mongolian Red = 2.7\r
Mountain White = 2.7\r
Dragon Beige = 2.7\r
etc.etc.
Then I try:
>>> for entry in x:
x,y = string.split(entry,"=")
Traceback (innermost last):
File "<pyshell#24>", line 2, in ?
x,y = string.split(entry,"=")
ValueError: unpack list of wrong size
Notice all the '\r' entries.
Anyone know what's going on and what I have to do. Do I have to convert
all my scripts using dos2unix?
TIA,
Jethro