几个小时以来,我一直在尝试从1-8编号的.txt文件中复制问题,然后将每个问题粘贴到自己单独的.txt文件中,其名称为:(1.txt,2.txt,3.txt等。 )。
这是.txt文件:
1. (I) If the coefficient of kinetic friction between a 22-kg crate and the floor is 0.30, what horizontal force is required to move the crate at a steady speed across the floor? What horizontal force is required if /Lk is zero? 2. (I) A force of 35.0 N is required to start a 6.0-kg box moving across a horizontal concrete floor. (a) What is the coefficient of static friction between the box and the floor? (b) If the 35.0-N force continues, the box accelerates at 0.60 m/s 2. What is the coefficient of kinetic friction? 3. (I) Suppose you are standing on a train accelerating at 0.20 g. What minimum coefficient of static friction must exist between your feet and the floor if you are not to slide? 4. (I) The coefficient of static friction between hard rubber and normal street pavement is about 0.90. On how steep a hill (maximum angle) can you leave a car parked? 5. (I) What is the maximum acceleration a car can undergo if the coefficient of static friction between the tires and the ground is 0.90? 6. (II) (a) A box sits at rest on a rough 33° inclined plane. Draw the free-body diagram, showing all the forces acting on the box. (b) How would the diagram change if the box were sliding down the plane. (c) How would it change if the box were sliding up the plane after an initial shove?我尝试循环遍历列表并使用if语句,绝对没有运气。 我已经无数次开始,但这是我最新的代码:
n = 1 stuff = open("copy.txt","r+") new_file = "C:/Users/me/Desktop/DummyDirectory/" + str(n) + ".txt" destination = open(new_file,"a") n = 1 question_number = str(n) + ". " #note: there is a space next to the decimal question_number_plus_1 = str(n + 1) + ". " for the_line in stuff: reached_question_number = question_number in the_line while n < 11: if reached_question_number: for the_line in stuff: if question_number_plus_1 not in the_line: destination.write(the_line) elif question_number_plus_1 in the_line: pass break n += 1 stuff.seek(0)对不起,如果这段代码没有任何意义。
For hours, I have been trying to copy questions from a .txt file numbered from 1-8 and paste each question into its own separate .txt file with the names: (1.txt, 2.txt, 3.txt, etc.).
Here is the .txt file:
1. (I) If the coefficient of kinetic friction between a 22-kg crate and the floor is 0.30, what horizontal force is required to move the crate at a steady speed across the floor? What horizontal force is required if /Lk is zero? 2. (I) A force of 35.0 N is required to start a 6.0-kg box moving across a horizontal concrete floor. (a) What is the coefficient of static friction between the box and the floor? (b) If the 35.0-N force continues, the box accelerates at 0.60 m/s 2. What is the coefficient of kinetic friction? 3. (I) Suppose you are standing on a train accelerating at 0.20 g. What minimum coefficient of static friction must exist between your feet and the floor if you are not to slide? 4. (I) The coefficient of static friction between hard rubber and normal street pavement is about 0.90. On how steep a hill (maximum angle) can you leave a car parked? 5. (I) What is the maximum acceleration a car can undergo if the coefficient of static friction between the tires and the ground is 0.90? 6. (II) (a) A box sits at rest on a rough 33° inclined plane. Draw the free-body diagram, showing all the forces acting on the box. (b) How would the diagram change if the box were sliding down the plane. (c) How would it change if the box were sliding up the plane after an initial shove?I tried looping through the list and using if statements, with absolutely no luck. I have started over countless times but here is my latest code:
n = 1 stuff = open("copy.txt","r+") new_file = "C:/Users/me/Desktop/DummyDirectory/" + str(n) + ".txt" destination = open(new_file,"a") n = 1 question_number = str(n) + ". " #note: there is a space next to the decimal question_number_plus_1 = str(n + 1) + ". " for the_line in stuff: reached_question_number = question_number in the_line while n < 11: if reached_question_number: for the_line in stuff: if question_number_plus_1 not in the_line: destination.write(the_line) elif question_number_plus_1 in the_line: pass break n += 1 stuff.seek(0)Sorry if this code doesn't make any sense.
最满意答案
查看文本文件,有两种类型的行...
以数字开头然后是"." 不以数字开头然后是"."后一类行总是与第一类连接。
我们可以用它来提出算法。
如果该行以数字和点开头,则关闭当前打开的文件(如果存在),打开一个新文件,将该行写入该文件。 如果该行不以数字和点开头,请将该行写入当前打开的文件。你如何实现这一点取决于你 - 但我喜欢将它们视为组,所以我建议使用itertools.groupby :
import itertools import re NUM_RE = re.compile(r'(^\d+\.)') def get_line_num(line): match = NUM_RE.match(line) if match: return match.group(1) with open('questions.txt', 'r') as text: file_obj = None for line_num, group in itertools.groupby(text, key=get_line_num): if line_num: if file_obj: file_obj.close() file_obj = open(line_num + 'txt', 'w') file_obj.writelines(group) else: file_obj.writelines(group) file_obj.close()当然,这可以变得更加健壮(在"1."问题之前有文件的文件怎么样?)...由于这种行为未指定,我将在此处未指定;-)
Looking at your text file, there are two types of lines...
Lines that start with a number and then a "." Lines that do not start with a number and then a "."The latter class of lines always is meant to be joined with the first class.
We can use this to come up with an algorithm.
If the line starts with a number and a dot, close currently open file (if present), open a new file, write the line to it. If the line does not start with a number and a dot, write the line to the currently open file.How you implement this is up to you -- But I like to think of these as groups, so I'd recommend using itertools.groupby:
import itertools import re NUM_RE = re.compile(r'(^\d+\.)') def get_line_num(line): match = NUM_RE.match(line) if match: return match.group(1) with open('questions.txt', 'r') as text: file_obj = None for line_num, group in itertools.groupby(text, key=get_line_num): if line_num: if file_obj: file_obj.close() file_obj = open(line_num + 'txt', 'w') file_obj.writelines(group) else: file_obj.writelines(group) file_obj.close()Of course, this could be made more robust (what about files where there is header text before the "1." question?)... Since that behavior is unspecified, I'll leave it unspecified here too ;-)
更多推荐
发布评论