Home > other >  Python problems seek help from a great god
Python problems seek help from a great god

Time:11-28

1. Niuniu file extractor (6 points)
Niuniu in the process of handling file found a file extractor app, the app can read both Chinese and English mixed files, and to pick up the inside of English, as a new file, but one day he found his own app trial period is over, we may not be able to use, please help him realize the function of this app, let him be able to deal with the files quickly,

CodePudding user response:

 with the open (' input. TXT) as fp: 
String=fp. Read ()
With the open (' output. TXT ', 'w') as fp:
Fp. Write ('. Join (char for char string in the if word (char) & lt; 128))

May need to be in oepn function parameters and encoding="utf-8" or encoding="GBK encoding specified file

CodePudding user response:

Didn't understand that, a great god

CodePudding user response:

The for loop a indentation
 with the open (' input. TXT, encoding="utf-8") as fp: 
String=fp. Read ()
With the open (' output. TXT ', 'w') as fp:
Fp. Write ('. Join (char for char string in the if word (char) & lt; 128))

CodePudding user response:

happy pigs reference 3 floor response:
for loop a indentation
 with the open (' input. TXT, encoding="utf-8") as fp: 
String=fp. Read ()
With the open (' output. TXT ', 'w') as fp:
Fp. Write ('. Join (char for char string in the if word (char) & lt; 128))

List comprehensions don't need to indent

CodePudding user response:

refer to the second floor suao123 response:
didn't understand that, a great god

Read the file, and then use the generator expressions (char for char string if in word (char) & lt; 128) filtering (including word (char) & lt; 128 is condition), only the ASCII characters in English, because the generator expressions do not return a string, so using '. Connect the join, converted to a string, and then written to the output file

CodePudding user response:

refer to the second floor suao123 response:
didn't understand that, a great god

Read the file, and then use the generator expressions (char for char string if in word (char) & lt; 128) filtering (including word (char) & lt; 128 is condition), only the ASCII characters in English, because the generator expressions do not return a string, so using '. Connect the join, converted to a string, and then written to the output file

CodePudding user response:

You are not understand don't understand that part or all,
The problem solving steps I write to you:
1. Read the text ()
2. To filter out Chinese (regular expressions or great god wrote the)
3. The filtered words written text
  • Related