我有一个文字文件
Mash,25.50,108.00
Enhanced,37.50,162.00
Pellets,30.00,135.00
Protein Plus,39.00,174.00
Calcium Plus,39.00,174.00
我将其运行到打印语句中
def readfile():
textlist = [line.split(',') for line in open("chookfood.txt", 'r')]
print(','.join([str(lst) for lst in textlist]))
readfile()
它输出此嵌套列表:
['Mash', '25.50', '108.00\n'],['Enhanced', '37.50', '162.00\n'],['Pellets', '30.00', '135.00\n'],['Protein Plus', '39.00', '174.00\n'],['Calcium Plus', '39.00', '174.00']
如何制作打印语句以打印mash [1]。我无法将这些值存储在闲置的python中,所以我需要某种方式来询问如何从mash中打印25.50?
答案 0 :(得分:1)
您已经具有包含内容的列表列表。您只需要从函数返回列表,然后使用列表索引访问内容:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use DB;
use App\Booking;
class Expire extends Command
{
protected $signature = 'log:demo';
protected $description = 'changer la date de sortie';
public function __construct()
{
parent::__construct();
}
public function handle()
{
//
DB::table('bookings')->whereDate('sortie', '<=', \Carbon\Carbon::now())->update(['statut' => 'disponible']);
}
}
然后,仅使用列表索引:
<?php
namespace App\Console;
use DB;
use App\Booking;
use \Carbon\Carbon;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected $commands = [
'App\Console\Commands\Expire'
];
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
$schedule->command('log:demo')
->everyMinute();
}
protected function commands()
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
答案 1 :(得分:0)
如果它们是逗号分隔的值,请使用csv
模块:
import csv
with open('chookfood.txt') as chookfood:
rows = csv.reader(chookfood)
for row in rows:
print(row[1])
答案 2 :(得分:0)
您可以更改初始列表以将其存储在字典中:
def readfile():
dictionary = { data[0]: [float(data[1], float(data[2])] for data in [line.split(',') for line in open("chookfood.txt", 'r')]
print(dictionary)
您可以通过编写以下内容来访问Mash的第一个元素:
print(dictionary[“Mash”][0])