f = open("t.txt")
data= list()
for line in f:
data = place("\n","")
print(data)
data = eval(data)
print(data)
print(data[0])
f.close()
123,23,2,1,2,3
(123, 23, 2, 1, 2, 3)
123
3,2,3,23,123
(3, 2, 3, 23, 123)
3
3,3,3,3,2,2,2
(3, 3, 3, 3, 2, 2, 2)
3
4,4,4,4,4,4,4,4
(4, 4, 4, 4, 4, 4, 4, 4)
4
5,5,5,5,5,5,5,5
(5, 5, 5, 5, 5, 5, 5, 5)
5
import turtle as t
t.title('⾃动绘制轨迹')
t.setup(800,600,0,0)
t.pencolor("red")
t.pensize(5)
def tprint(a):
t.pencolor(a[3],a[4],a[5])
t.fd(a[0])
if a[1]:
t.right(a[2])
else:
t.left(a[2])
f = open("t.txt")
data= list()
for line in f:
if len(line) == 1:
break;
data = place("\n","") #去掉每⼀⾏的换⾏#print(data)
data = eval(data)  #转换为元组
#print(data)
#print(data[0])
tprint(data)    #元组传参
f.close()
#以下为t.txt⽂本⽂件内容
2021\10\30;
不说精通,但也算灵活运⽤了;
.txt
11,12,13
21,22,23
31,32,33
41,42,43
one,two,three
file = open('⽂本.txt')
all_data=[]
for line in file:
temp = line.split(',')
all_data.append(temp)
print('-----------------------------')
try:
print(all_data)
print(all_data[0])
print(all_data[-1][0])
except:
pass
all_data.clear()
print('=============================')
file.seek(0)
for line adlines():
temp = place('\n',"").split(',') #去掉换⾏以逗号分隔
all_data.append(temp)
try:
print(all_data)
print(all_data[0])
print(all_data[-1][0])
except:
pass
python怎么读取txt
print('----------------------------')
if int(all_data[0][0])==11:
print(all_data[0])
PS E:\python\python> & "E:/Program Files/" e:/python/python/python_all_by_me/⽂本处理/分割字符.py -----------------------------
[['11', '12', '13\n'], ['21', '22', '23\n'], ['31', '32', '33\n'], ['41', '42', '43\n'], ['one', 'two', 'three']]
['11', '12', '13\n']
one
=============================
[['11', '12', '13'], ['21', '22', '23'], ['31', '32', '33'], ['41', '42', '43'], ['one', 'two', 'three']]
['11', '12', '13']
one
----------------------------
['11', '12', '13']

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。