python读取txt的数据 并再次写入语言中

如题,
假设txt档中的数据为
100, 200, 300, 400
10, 20, 30, 40
78, 88, 89, 99
(后面还有其他)
我想要从这些里面由第一行开始只读取100, 200, 400;换第二行开始一恙只读取10, 20, 40
并写入到以下的文件中(nodes[])
node1 = instance['pop'].nodes[100]
node2 = instance['pop'].nodes[200]
node3 = instance['pop'].nodes[400]
78, 88, 99亦同,并做一个回圈来产出

使用open函数打开文件,返回文件句柄
使用文件句柄的read方法读取文件内容
f = open('/path/to/the/file.txt')
txt = f.read()
txt文件的内容将会读取待txt变量中
温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-06-14
文件 = open("文件名.txt","r").read()
print(文件)

这样即可读取出txt文件的内容哦   至于 要写入什么语言中 直接嵌入进入即可

第2个回答  2016-06-03

    

with open('path_to_txt.txt', 'rw') as f:
    lines = f.readlines()
    for line in lines:
        # 对每一行进行操作
    f.write() # 写入你想要的东西

 

第3个回答  2016-07-28
with open('路径', 'r') as f:    
    content = f.read()
with open('路径', 'w') as f:    
    f.write(content)
路径和txt文件存在就能执行了

第4个回答  2016-07-04
with open('a.txt') as f:
content = f.read() # 获取文本内容
f.write('写入文本的内容')
相似回答