[英]How write python to Read the first two lines from a text file named "file1.txt" Write the two lines read from "file1.txt" to a new file "file2.txt"
[英]How read the correct lines from this text file with a python program, and then create a .py file by filling in the data extracted from the .txt file?
要讀取的文本文件(真實的包含更多的數字),名為number_info.txt
veinti tres
23
veinti dos
22
veinti uno
21
veinte
20
tres
3
dos
2
uno
1
這是代碼(我需要幫助)
import re
def auto_coding_text_to_number():
with open('number_info.txt', 'r') as f:
#lines 0, 3, 6, 9, 12, 15, 18, ...
coloquial_numbers = []
#lines 0+1, 3+1, 6+1, 9+1, 12+1, 15+1, 18+1, ...
symbolic_numbers = []
n = 0
with open('number_to_text.py', 'w') as f:
f.write('import re\n\ndef number_to_text_func(input_text):\n')
#write replacement lines based on regex
if(" " in coloquial_numbers[n]):
#for example write this line: " input_text = re.sub(r"veinti[\s|-|]*tres", "23", input_text)"
if not (" " in coloquial_numbers[n]):
#for example write this line: " input_text = re.sub("tres", "3", input_text)"
f.write(" return(input_text)\n input_text = str(input())\n
print(number_to_text_func(input_text))")
n = n + 1
auto_coding_text_to_number()
這是正確的文件,名為number_to_text.py
,應該由其他腳本編寫
import re
def number_to_text_func(input_text):
input_text = re.sub(r"veinti[\s|-|]*tres", "23", input_text)
input_text = re.sub(r"veinti[\s|-|]*dos", "22", input_text)
input_text = re.sub(r"veinti[\s|-|]*uno", "21", input_text)
input_text = re.sub("tres", "3", input_text)
input_text = re.sub("dos", "2", input_text)
input_text = re.sub("uno", "1", input_text)
return(input_text)
input_text = str(input())
print(number_to_text_func(input_text))
編輯:
.txt 文件中的行結構如下
"veinti tres" <---- line 0
"23" <---- line 1
"veinti dos" <---- line 2
"22" <---- line 3
"veinti uno" <---- line 4
"21" <---- line 5
"veinte" <---- line 6
"20" <---- line 7
"tres" <---- line 8
"3" <---- line 9
然后我建議將它們分成 2 組並將它們存儲在 2 個列表中
#lines 0, 3, 6, 9, 12, 15, 18, ...
coloquial_numbers = ["veinti tres", "veinti dos", "veinti uno", "veinte", "tres"]
#lines 0+1, 3+1, 6+1, 9+1, 12+1, 15+1, 18+1, ...
symbolic_numbers = ["23", "22", "21", "20". "3"]
body_template = """ input_text = re.sub(r"{}", "{}", input_text)\n"""
然后 function 的主體應該是這樣的結構
input_text = re.sub(coloquial_numbers[n].replace(' ', '[\s|-|]'), symbolic_numbers[n], input_text)
在 output 文件的 function 正文中得到類似的東西
def number_to_text(input_text):
input_text = re.sub(r"veinti[\s|-|]*tres", "23", input_text)
input_text = re.sub(r"veinti[\s|-|]*dos", "22", input_text)
input_text = re.sub(r"veinti[\s|-|]*uno", "21", input_text)
input_text = re.sub("tres", "3", input_text)
return(input_text)
為了簡單起見,我省略了讀/寫步驟。 沒有給出指定元 function 主體的規則,所以我做了一個猜測。
import re
# body-component of the meta-code
body_template = """ input_text = re.sub(r"{}", "{}", input_text)\n"""
# read from file
with open('number_info.txt', 'r') as fd:
text = fd.read()
# update body
body = ''
for n_text, n in re.findall(r'\n*([a-z\s]+)\n(\d+)', text):
body += body_template.format(n_text.replace(' ', '[\s|-|]'), n)
# other components of the meta-code
header = """import re
def number_to_text_func(input_text):
"""
tail = """\n return(input_text)
input_text = str(input())
print(number_to_text_func(input_text))"""
# merge together texts to be saved to file
meta_code = header + body + tail
print(meta_code)
Output( number_to_text.py的內容)
import re
def number_to_text_func(input_text):
input_text = re.sub(r"treinta[\s|-|]y[\s|-|]uno", "31", input_text) # <-
input_text = re.sub(r"veinti[\s|-|]tres", "23", input_text)
input_text = re.sub(r"veinti[\s|-|]dos", "22", input_text)
input_text = re.sub(r"veinti[\s|-|]uno", "21", input_text)
input_text = re.sub(r"veinte", "20", input_text)
input_text = re.sub(r"tres", "3", input_text)
input_text = re.sub(r"dos", "2", input_text)
input_text = re.sub(r"uno", "1", input_text)
return(input_text)
input_text = str(input())
print(number_to_text_func(input_text))
來自評論:每行讀取文件,沒有正則表達式
with open('number_info.txt', 'r') as fd:
lines = fd.read().split()
symbolic_numbers, coloquial_numbers = [], []
for i, line in enumerate(lines):
if i % 3 == 0:
coloquial_numbers.append(line)
elif i % 3 == 1:
symbolic_numbers.append(line)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.