On Friday, 19 February 2016 03:06:58 UTC+2, Steven D'Aprano  wrote:
> On Fri, 19 Feb 2016 02:35 am, wrong.addres...@gmail.com wrote:
> 
> > I am almost eager to do this but want to be sure that I know the pitfalls
> > in using Python for my purposes. Thanks for your encouraging response.
> 
> Honestly "wrong.address.1", the ONLY pitfall you need to be aware of is to
> beware of trying to write VB code using Python's interpreter. Python has
> it's own way of doing things, and if you insist on doing them "just like
> VB" you will end up with horrible, slow, inefficient, unusable code. 

This I am aware of. And it is not a problem.

> That's
> not to say that VB is worse, or Python is worse, just that they are
> different. You don't use a hammer the same was a screwdriver.
> 
> Python is a 20+ year old language used by millions of professionals all over
> the world for everything from quick scripting, scientific computing, web
> applications, long-running server applications, and everything in between.
> The answer to every one of your questions "Can Python do X?" will be one
> of:
> 
> (1) Yes it can.
> 
> (2) No, but instead it will do Y which gives you the same result.
> 
> 
> I'll be frank, to come here and ask a bunch of questions like:
> 
> "Is it necessary to read one character at a time...?"
> 
> is a little bit rude. I don't want to discourage you from asking questions,
> but think about *how* you ask them. What you're doing is a little bit like
> going to a car dealer, looking at the cars, then asking a bunch of
> questions:

No, I first saw this trouble in VB.net. Later I found there was an input 
command which allowed me to read numbers in varying formats more or less like 
in Fortran or Basic. I really hope Python has decent ways of reading numeric 
data which may not follow regular formats.

> 
> "So... does this car have a reverse gear or can it only go forward? Do the
> doors open for entry, or do I have to squeeze through the windows? Can I
> use the radio while driving, or is there only enough power for one at a
> time?"
> 
> In most programming communities, if you start asking questions like that,
> you can expect to be ignored or abused. Good thing we're a friendly
> bunch :-)

I hope it was not rude. At least I did not mean to be. 

> 
> 
> Instead, a good question is:
> 
> "How do I read a bunch of numbers from a text file?"
> 
> 
> I'll show you, not only how to read a bunch of numbers, but how to write
> them first.
> 
> Of course there are a million different ways you can do this, and for
> serious use you will probably want to use something like a CSV (Comma
> Separated Values) file like Excel produces, but for a quick idea of how
> Python works, let's write some numbers to a file, one per line. Comments
> start with # and have no effect. Remember that indentation is meaningful:
> you must use the same number of spaces as shown in my code.
> 
> Copy and paste this code into the Python interpreter:
> 
> 
> 
> # ===== cut =====
> 
> # Define some numbers.
> numbers = [1, 45, 38, 99, 1002, 83025, 234, 55, 273, 2]
> # Open a file for writing.
> with open("myfile.txt", "w") as f:
>     # Write each number to the file, one per line.
>     for number in numbers:
>         print("Writing %d to the file." % number)
>         f.write(str(number) + "\n")
> 
> # ===== cut =====
> 
> 
> 
> And that's done. Five lines of code, ignoring comments. Now let's read them
> back and see if they're the same:
> 
> 
> # ===== cut =====
> 
> # Prepare a list to hold the numbers.
> data = []
> # Open a file for reading.
> with open("myfile.txt", "r") as f:
>     # Read each line.
>     for line in f:
>         value = line.strip()  # Get rid of trailing newline.
>         print("Read %s from the file." % value)
>         data.append(int(value))
> 

I have not yet learnt any Python so I do not understand almost anything. 
Besides, I will have to read data given to me by people, which may not come in 
nice formats like CSV, or pre-written by Python.

> # Confirm the numbers read in are the same as those written out.
> if data != numbers:
>     print("mismatch in values")
> 
> # Print the sorted values.
> print(sorted(data))
> 
> # ===== cut =====
> 
> 
> 
> Does this help?
> 

Yes. Thanks for your response. I am clear about that I will have to think in a 
different way and not try to convert VB to Python line by line.

> 
> 
> -- 
> Steven

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to