python将文件内容放在列表里,读入一个.txt文件并将元素放入列表中(Python)

Im new in Python and i need some help. i got a .txt file in this form:

Time[tab]Signal

0[tab]1.05

0.5[tab]1.06

1[tab]1.09

1.5[tab]1.12

Now i want to read in the file. I need two lists. List1 should contain the time and list2 should contain the signal.

This is my try:

daten = open("extedit.txt", "r")

lines = daten.readlines();

list1 = []

for i in lines:

list1.append(i.strip().split('\t'));

daten.close()

del list1[1]

print(list1)

c3e8d5815c528d91cfec2b92afe1bdd6.png

Something went wrong i think.. maybe you can help me

i got this in my terminal:

[['{\rtf1\ansi\ansicpg1252\cocoartf1504\cocoasubrtf760'], ['{\colortbl;\red255\green255\blue255;}'], ['{\*\expandedcolortbl;;}'], ['\paperw11900\paperh16840\margl1440\margr1440\vieww10800\viewh8400\viewkind0'], ['\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\pardirnatural\partightenfactor0'], [''], ['\f0\fs24 \cf0 Zeit', 'Signal\'], ['0.01', '1.1\'], ['0.02', '1.105\'], ['0.03', '1.108\'], ['0.04', '1.2\'], ['0.05', '1.205\'], ['0.06', '1.209}']]

解决方案

don't use semicolons in python!

As suspected, you are trying to read a RTF file.

First reformat your file. I propose also an other way:

import csv

list1 = []

with open("extedit.txt") as tsv:

for line in csv.reader(tsv, dialect="excel-tab"):

list1.append(line[0])

del list1[0]

print(list1)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值