python遍历一个txt文本,里面有100条数据,数据的类型是12353: [22890, 37446,37449,37450],希望输出的结果是[('12353',[('22890','37446','37449', '37450')])]
时间: 2023-10-23 18:24:15 浏览: 35
假设txt文本中每行都是一条数据,可以使用以下代码实现:
```python
result = []
with open('data.txt', 'r') as f:
for line in f:
key, values = line.strip().split(': ')
values = tuple(values.strip('[]').split(','))
result.append((key, values))
result = [(result[0][0], [result])] if result else []
print(result)
```
解释:
1. 打开文件并逐行读取数据。
2. 对于每行数据,使用 `strip()` 方法去除首尾空格,然后使用 `split()` 方法将键和值分开。
3. 对于值部分,使用 `strip()` 方法去除首尾方括号,然后使用 `split()` 方法将每个元素分开,并转换为元组。
4. 将键和值组成一个元组,然后添加到结果列表中。
5. 最后,如果结果列表不为空,则将其转换为输出格式,否则输出空列表。
相关问题
python遍历txt文本,有多条数据,数据是12353: [22890, 37446, 37449, ‘37450B’]这种类型,输出(‘12353’, [('22890'),(' 37446'), ('37449'), ('37450B')]),的格式
可以使用Python的文件读取和字符串处理方法来实现。体步骤如下:
1. 打开txt文件,并逐行读取数据。
2. 对于每一行数据,使用字符串的split方法将其按照冒号分割为两个部分,即键和值。
3. 对于值部分,使用字符串的replace方法将其中的方括号和单引号去掉,并使用split方法将其按照逗号分割成多个字符串。
4. 将键和处理后的值组成一个元组,添加到一个列表中。
5. 最终输出列表中每个元素的格式为(‘键’, [(‘值1’), (‘值2’), …])。
以下是一个示例代码:
```python
result = []
with open('data.txt', 'r') as f:
for line in f:
key, value = line.strip().split(': ')
value = value.replace('[', '').replace(']', '').replace('\'', '').split(', ')
value = [(v,) for v in value]
result.append((key, value))
print(result)
```
假设data.txt文件中的数据为:
```
12353: [22890, 37446, 37449, '37450B']
45678: [123, '456', 789]
```
则输出结果为:
```
[('12353', [('22890',), ('37446',), ('37449',), ('37450B',)]), ('45678', [('123',), ('456',), ('789',)])]
```
python 遍历txt文本的多条数据,其中数据类型是123: [a,b],[c,d],最终输出[('123', [('a','b'), ('c','d')])] ,
假设txt文本中每行只有一条数据,可以使用以下代码实现:
```
result = []
with open('data.txt', 'r') as f:
for line in f:
data = line.strip().split(':')
key = data[0]
values = data[1].split(',')
sub_result = []
for value in values:
sub_data = value.strip('[]').split(',')
sub_result.append((sub_data[0], sub_data[1]))
result.append((key, sub_result))
print(result)
```
其中,假设txt文本中的数据格式为:`123: [a,b],[c,d]`,则代码中的`data = line.strip().split(':')`将数据按照冒号分隔开,并获取到key和values。接着,使用`values = data[1].split(',')`将values按照逗号分隔开,得到一个包含多个子数据的列表。然后,使用一个循环遍历每个子数据,将其按照逗号和方括号分隔开,并将分隔后的数据存入一个元组中。最后,将key和子数据列表组成一个元组,并添加到结果列表中。最终输出的结果即为`[('123', [('a','b'), ('c','d')])]`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)