简体   繁体   English

如何使用info from.txt文件在python中创建变量?

[英]how to use info from .txt file to create variables in python?

I'm very new to python, and I'd like to know how I can use the info in a text file to create variables.我是 python 的新手,我想知道如何使用文本文件中的信息来创建变量。 For example, if the txt file looked like this:例如,如果 txt 文件如下所示:

vin_brand_type_year_price
2132_BMW_330xi_2016_67000 
1234_audi_a4_2019_92000 
9876_mclaren_720s_2022_327000 

How do I then, for example, use it to make a variable called vin and have all the vin numbers in it?例如,我如何使用它创建一个名为 vin 的变量并将所有 vin 编号包含在其中?

I can have the terminal read it.我可以让终端读取它。 this is what i have so far这是我到目前为止所拥有的

with open('car.txt', 'r') as file:
    file_content = file.read()
    print(file_content)

There are several ways to do this.有几种方法可以做到这一点。 The best depends on what you plan to do next.最好的取决于你下一步打算做什么。 This file will parse with the csv module and you can use csv.reader to iterate all of the lines.该文件将使用csv模块进行解析,您可以使用csv.reader迭代所有行。 To get vin specifically, you could要专门获得vin ,您可以

import csv

with open('car.txt', 'r') as file:
    next(file) # drop header
    vin = [row[0] for row in csv.reader(file, delimiter="_")]

I would use regex to accomplish that.我会使用正则表达式来完成它。 Assuming the file (car.txt) looks like this:假设文件 (car.txt) 如下所示:

vin_brand_type_year_price
2132_BMW_330xi_2016_67000
1234_audi_a4_2019_92000
9876_mclaren_720s_2022_327000

I would use this python script:我会使用这个 python 脚本:

import re

with open('car.txt') as f:
    data = f.readlines()

vin = []
for v in data:
    if match := re.match(r'(\d+)', v.strip()):
        vin.append(match.group(0))

print(vin)

the

r'^(\d)+' r'^(\d)+'

is a regex for selecting the part of the text that starts with digits.是一个正则表达式,用于选择以数字开头的文本部分。 This is to ensure any line in the file that doesn't start with digits will be ignored.这是为了确保文件中不以数字开头的任何行都将被忽略。

You can slice the strings around '_', get the first part (at index 0) and append it to a list variable:您可以将“_”周围的字符串slice ,获取第一部分(在索引 0 处)并将其 append 到列表变量:

vin = []

with open('car.txt', 'r') as file:
    lines = file.readlines()    
for line in lines.splitlines():
    line = line.strip()
    if line:
        vin.append(line.split('_')[0])
        
vin.pop(0) # this one because I was too cheap to skip the header line :)

Here is a method to make a dict of the values and treat the first row as the header:下面是一种方法,可以将值字典化,并将第一行视为 header:

with open(your_file) as f:
    header=next(f).rstrip().split('_')
    data={}
    for row in f:
        for k, v in zip(header, row.rstrip().split('_')):
            data.setdefault(k, []).append(v)

Or, you can use some * magic and more succinctly do:或者,您可以使用一些*魔法并更简洁地执行以下操作:

with open(your_file) as f:
    data={k:v for k,*v in (zip(*[e.rstrip().split("_") for e in f]))}

Either results:结果:

>>> data
{'vin': ['2132', '1234', '9876'], 'brand': ['BMW', 'audi', 'mclaren'], 'type': ['330xi', 'a4', '720s'], 'year': ['2016', '2019', '2022'], 'price': ['67000', '92000', '327000']}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM