如何使用Python读取文本文件中的数组? [英] How to read array inside text file using Python?

查看:840
本文介绍了如何使用Python读取文本文件中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由数组组成的文本文件。这是我的档案:

I have a text file consist of array. This is my file :

["fr", "fs", "ft", "fu"]

What I have tried:

<pre lang="Python">
file = array.txt
with open(file, "r") as input_file:

print (input_file)

推荐答案

您可以使用 eval 来达到目的:

You might use eval for the purpose:
file = "array.txt"
with open(file, "r") as input_file:
  the_list = eval(input_file.read())
# let's show we've really a list!
for (i,v) in enumerate(the_list):
  print("the_list[{0}] = {1}".format(i,v))


这篇关于如何使用Python读取文本文件中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆