Home > other >  Will file all except the reserved words lowercase letters to uppercase
Will file all except the reserved words lowercase letters to uppercase

Time:09-27

Homework problems: write a program that reads a Python source file, the file of all except the reserved words lowercase letters for capital letters, generated file is to be able to the Python interpreter later executed correctly,

I chose the written a program that source file named: 7.3 py, code is as follows:
# # write a program to generate a 10 x 10 random matrix is saved in the file
# # a whitespace-delimited row vector, line segmentation column
# # put this matrix file save as CSV format
Import the random
F=open (' 7.3. TXT ', 'w')
For y in range (10) :
For the x in the range (10) :
Randnums=STR (random randint (0100 0))
If x!=9:
F.w rite (' {} '. The format (randnums + '))
Elif x==y==9:
F.w rite (randnums)
The else:
F.w rite (' {} '. The format (randnums + '\ n'))
F. lose ()

Fr=open (' 7.3. TXT ', 'r'). The read ()
FCSV=open (' 7.3. CSV ', 'w')
FCSV. Write (fr. Replace (', ', '))
FCSV. Close ()


Personal thinking: if the change the reserved words, built-in functions and internal parameters of the case, to regenerate the program will not perform well, can only change the custom variable name or function name (and parameters),
And only change the custom variable name or function name (and parameters) case, and do not conform to the question (will file all except the reserved words lowercase letters for capital letters),

Racking their brains to match the idea didn't write code,
Baidu for a long time and still didn't find the right answer.

Was the feeling, consult a great god given in accordance with the question and the source code to solve!

CodePudding user response:

Can write change reserved words, built-in functions and built-in parameters of other code can also letter case,

CodePudding user response:

Said this problem, simple, and simple, said difficult, particularly difficult, is very difficult to achieve

Train of thought is that the word segmentation, separate source code form, judgment, not a reserved word and built-in functions are capitalized
For reserved word and built-in functions

 
The import keyword
Print (keyword. Kwlist)
Print (dir (__builtins__))


Why say difficult, because in addition to these there are some third-party libraries, such as the poor judgment, that has implications for distinguish in the import and custom
To judge whether a function is a custom, custom is no problem to uppercase
About it is difficult that I said, if you don't consider it is just like that

Here give you an idea
Python statements, all has been running In ram will be kept In the global In inside, you can print In, or globals () to see


This for yourself, you feel me to look down and can directly open the blog

CodePudding user response:

Part drawing curve drawing is wrong, should be In underneath

CodePudding user response:

This reminds me of the compiling principle of this program is not good to

CodePudding user response:

The
cited the 4th floor old coconut reply:
this reminds me of the compiling principle, the program is not good to

Ha, ha, ha, the homework problems, this lesson can be for a lifetime

CodePudding user response:

Simple processing, reference this article

https://blog.csdn.net/qq_43238969/article/details/102896792

  • Related