从列表中读书

发布于 2025-01-26 21:10:22 字数 671 浏览 2 评论 0原文

import numpy as np

filename = 'input1.txt'
output_file = 'output.txt'
delimiters = ',.?!'

with open(filename, 'r') as f:
    text = f.read()
for delimiter in delimiters:
    text = text.replace(delimiter, ',')
lines = text.split('\n')
parts = list()
print(lines)

我希望所有包含文本 /数字的行都在表示

例中,我有一个输入文件。文本文件,

1,2,3,4,5,6
7,8,9,10,11,12
13,14,15,16,17

['1,2,3,4,5,6', '7,8,9,10,11,12', '13, 14,15,16,17 ',' ',' ',' ', '', '', '', '', '']

正在寻找从后面的退出来阅读行,

['13, 14,15,16,17', '7,8,9,10,11,12', '1,2,3,4,5,6', '', '', '', '', '', '', '']

谢谢您的帮助

import numpy as np

filename = 'input1.txt'
output_file = 'output.txt'
delimiters = ',.?!'

with open(filename, 'r') as f:
    text = f.read()
for delimiter in delimiters:
    text = text.replace(delimiter, ',')
lines = text.split('\n')
parts = list()
print(lines)

I want all the lines containing text / numbers from the back to be in the sheet

example I have an input.text file

1,2,3,4,5,6
7,8,9,10,11,12
13,14,15,16,17

this give me

['1,2,3,4,5,6', '7,8,9,10,11,12', '13, 14,15,16,17 ',' ',' ',' ', '', '', '', '', '']

I'm looking for an exit from behind to read the lines

['13, 14,15,16,17', '7,8,9,10,11,12', '1,2,3,4,5,6', '', '', '', '', '', '', '']

Thank you for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悸初 2025-02-02 21:10:22

在Python中反转列表:

print(list('abc')[::-1])
> ['c', 'b', 'a']

您的情况:

import numpy as np
import re
filename = 'input1.txt'
output_file = 'output.txt'
text = re.sub('[.?!]', ',', text)

with open(filename, 'r') as f:
    text = f.read()
text = re.sub('[.?!]', ',', text)

lines = text.split('\n')[::-1]
parts = []
print(lines)

Reverse a list in Python:

print(list('abc')[::-1])
> ['c', 'b', 'a']

Your case:

import numpy as np
import re
filename = 'input1.txt'
output_file = 'output.txt'
text = re.sub('[.?!]', ',', text)

with open(filename, 'r') as f:
    text = f.read()
text = re.sub('[.?!]', ',', text)

lines = text.split('\n')[::-1]
parts = []
print(lines)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文