对于想了解从Python文件中读取字典的书面列表的读者,本文将提供新的信息,我们将详细介绍python如何读取字典,并且为您提供关于PythonNumpy从文件中读取数据、python–从文本文件中读
对于想了解从Python文件中读取字典的书面列表的读者,本文将提供新的信息,我们将详细介绍python如何读取字典,并且为您提供关于Python Numpy 从文件中读取数据、python – 从文本文件中读取元组、python – 从文本文件中读取多个数字、python – 字典的子列表的有价值信息。
本文目录一览:- 从Python文件中读取字典的书面列表(python如何读取字典)
- Python Numpy 从文件中读取数据
- python – 从文本文件中读取元组
- python – 从文本文件中读取多个数字
- python – 字典的子列表
从Python文件中读取字典的书面列表(python如何读取字典)
我有一个这样的文件,它是字典的列表:
[{'text': 'this is the text','filed1': 'something1','filed2': 'something2','subtexts': [{'body': 'body of subtext1','date': 'xx'},{'body': 'body of
subtext2','date': 'yy'}}]
嵌套字典和列表的列表中可以有多个字典和列表。我想读取在python中完全像这样编写的文件,并创建一个数据结构(字典列表)。如何知道它不是json但已通过file.write(str(list))
我们想从文件中读取列表的位置将其写入文件?
Python Numpy 从文件中读取数据
测试文件内容(test1.txt)
hello,123,nihao
8,9,10
io,he,no
测试代码
import numpy
# dtype:默认读取数据类型,delimiter:分隔符
world_alcohol = numpy.genfromtxt("test1.txt", dtype=str, delimiter=",")
# 数据结构
print(type(world_alcohol))
# 数据内容
print(world_alcohol)
# 帮助文档
print(help(numpy.genfromtxt))
结果
<class ''numpy.ndarray''>
[[''hello'' ''123'' ''nihao'']
[''8'' ''9'' ''10'']
[''io'' ''he'' ''no'']]
...
python – 从文本文件中读取元组
这是我的txt:
(0,0) (0,0) (1,0) (2,3) (1,1) (1,1) (3,3) (2,2) (2,1) (4,4) (3,2) (3,1) (5,5)
我想逐一阅读这些列并获取元组列表:
尝试numpy我有这个:
import numpy as np File = np.genfromtxt('file.txt',delimiter=' ',dtype= tuple)
但它返回一个包含字节类型元素的列表列表.
显然我可以改变数据存储在txt中的方式.我只需要从txt获取元组列表(或列表列表).
解决方法
import re pattern='\((\d+,\d)\)' with open('demo.txt','r') as f: for line in f: data=re.findall(pattern,line) data_1=[] for item in data: data_1.append(tuple(map(lambda x:int(x),item.split(',')))) if data_1: print(data_1)
输出:
[(0,0),(0,(1,(2,3)] [(1,1),(3,3)] [(2,2),(4,4)] [(3,(5,5)]
甚至更好:
import re pattern='\((\d+,line) data_1=[tuple(map(lambda x:int(x),'))) for item in data] if data_1: print(data_1)
python – 从文本文件中读取多个数字
我有一个包含几个数字的文本文件:
12 35 21 123 12 15 12 18 89
我需要能够读取每行的单个数字,以便能够在数学公式中使用它们.
解决方法
with open("datafile") as f: for line in f: #Line is a string #split the string on whitespace,return a list of numbers # (as strings) numbers_str = line.split() #convert numbers to floats numbers_float = [float(x) for x in numbers_str] #map(float,numbers_str) works too
我已经完成了所有这一切的步骤,但你会经常看到人们组合它们:
with open('datafile') as f: for line in f: numbers_float = map(float,line.split()) #work with numbers_float here
最后,在数学公式中使用它们也很容易.首先,创建一个函数:
def function(x,y,z): return x+y+z
现在遍历你的文件调用函数:
with open('datafile') as f: for line in f: numbers_float = map(float,line.split()) print function(numbers_float[0],numbers_float[1],numbers_float[2]) #shorthand: print function(*numbers_float)
python – 字典的子列表
a = [["Hello","Bye"],["Morning","Night"],["Cat","Dog"]]
我想把它转换成字典.
我试过用:
i = iter(a) b = dict(zip(a[0::2],a[1::2]))
但它给了我一个错误:TypeError:unhashable type:’list’
解决方法
>>> a = [["Hello","Dog"]] >>> dict(a) {'Cat': 'Dog','Hello': 'Bye','Morning': 'Night'}
我喜欢python的简单性
您可以看到构建字典的所有方法的here:
To illustrate,the following examples all return a dictionary equal to
{"one": 1,"two": 2,"three": 3}
:
>>> a = dict(one=1,two=2,three=3) >>> b = {'one': 1,'two': 2,'three': 3} >>> c = dict(zip(['one','two','three'],[1,2,3])) >>> d = dict([('two',2),('one',1),('three',3)]) #<-Your case(Key/value pairs) >>> e = dict({'three': 3,'one': 1,'two': 2}) >>> a == b == c == d == e True
今天关于从Python文件中读取字典的书面列表和python如何读取字典的讲解已经结束,谢谢您的阅读,如果想了解更多关于Python Numpy 从文件中读取数据、python – 从文本文件中读取元组、python – 从文本文件中读取多个数字、python – 字典的子列表的相关知识,请在本站搜索。
本文标签: