Python:从文件中复制编号的问题并将它们粘贴到单独的文件中,并将编号作为每个文件的标题(Python: Copying numbered questions from a file and pas

编程入门 行业动态 更新时间:2024-10-28 08:20:37
Python:从文件中复制编号的问题并将它们粘贴到单独的文件中,并将编号作为每个文件的标题(Python: Copying numbered questions from a file and pasting them into separate files with the number as the title for each)

几个小时以来,我一直在尝试从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 ;-)

更多推荐

本文发布于:2023-08-01 18:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1364781.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并将   文件   编号   标题   numbered

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!