在Python中从选项卡文件中的特定行逐行读取
我试图从特定行的选项卡文件中读取信息,我希望逐行处理信息,以便创建包含这些行的列表。我读了文档,但没找到有用的东西 为了更清楚,我将举一个例子: 给定文件: 身份证对话在Python中从选项卡文件中的特定行逐行读取,python,Python,我试图从特定行的选项卡文件中读取信息,我希望逐行处理信息,以便创建包含这些行的列表。我读了文档,但没找到有用的东西 为了更清楚,我将举一个例子: 给定文件: 身份证对话 你好,你好吗 我很好,谢谢你的邀请 听到这个消息我很高兴 我只想阅读对话行并创建如下列表: list=['Hello,How are you', 'I am fine, thanks for asking', 'I am glad to hear that'] 下面是一个使用oneliner的示例: import io fi
list=['Hello,How are you', 'I am fine, thanks for asking', 'I am glad to hear that']
下面是一个使用oneliner的示例:
import io
file="""id conversation
1 Hello, How are you
2 I am fine, thanks for asking
3 I am glad to hear that"""
[' '.join(row.strip('\n').split(" ")[1:]) for row in io.StringIO(file).readlines() if row.strip('\n')][1:]
印刷品
['Hello, How are you',
'I am fine, thanks for asking',
'I am glad to hear that']
请添加您的代码您查看过这个吗?
import io
file="""id conversation
1 Hello, How are you
2 I am fine, thanks for asking
3 I am glad to hear that"""
with open("test.txt", "w") as f:
f.write(file)
with open("test.txt") as f:
mylist = [' '.join(row.strip('\n').split(" ")[1:]) for row in f.readlines() if row.strip('\n')][1:]
mylist