如何从.txt文件中减去我得到的两个值

时间:2016-04-13 20:59:05

标签: python csv

到目前为止,我已经设法在Python中打印出Html.Action()文件的某些部分,但我无法弄清楚如何从我的总金额中减去支付金额,然后将每列中的未付金额相加。

.txt

这是迄今为止的结果:

import csv

FILE_NAME = "paintingJobs.txt" #I use this so that the file can be used easier
COL_HEADERS = ['Number', 'Date', 'ID', 'Total', 'Status', 'Paid']
NUM_COLS = len(COL_HEADERS)#This will insure that the header of each column fits into the length of the data

# read file once to determine maximum width of data in columns
with open(FILE_NAME) as f:
    reader = csv.reader(f, delimiter=',')
    # determine the maximum width of the data in each column
    max_col_widths = [len(col_header) for col_header in COL_HEADERS]
    for columns in reader:
        for i, col in enumerate(columns):
            if "A" in columns and int(columns[5]) < int(columns[3]):
                max_col_widths[i] = max(max_col_widths[i], len(repr(col)))
    # add 1 to each for commas
    max_col_widths = [col_width+1 for col_width in max_col_widths]

# read file second time to display its contents with the headers
with open(FILE_NAME) as f:
    reader = csv.reader(f, delimiter=',')
    # display justified column headers
    print(' ' + ' '.join(col_header.ljust(max_col_widths[i])
                            for i, col_header in enumerate(COL_HEADERS)))
    # display justified column data
    for columns in reader:
        if "A" in columns and int(columns[5]) < int(columns[3]):
            print(columns)`

我想要做的是添加一个新列,它是总计和付费

的差异

2 个答案:

答案 0 :(得分:0)

看起来数据存储为字符串。您是否尝试将它们更改为整数?你会这样做的。假设我们有:

<?php
if(isset($_POST['submit'])) { 

    $conn = mysqli_connect("localhost", "root" , "");

    if(!$conn) { 
        die("Cannot connect: "); 
    }

    mysqli_select_db($conn,"blood_bank_project"); 

    $sql = "INSERT INTO user_account(username, password) VALUES ('$_POST[user]', '$_POST[psw]');";
    $sql .="INSERT INTO donor(first_name,last_name,email_add,gender, birthday, telephone, city, last_donation,user_id, blood_id)VALUES('$_POST[fname]', '$_POST[lname]', '$_POST[email]', '$_POST[gender]', '$_POST[Birthday]', '$_POST[Telephone]', '$_POST[city]', '$_POST[lastdonation]')";  
    $sql .="UPDATE donor SET blood_id = (SELECT blood_id from blood_type where blood_group= '$_POST[bloodgroup]');";  
    $sql .="UPDATE donor SET user_id = (SELECT user_id from user_account where username= '$_POST[user]')"; 

    if(mysqli_multi_query($conn, $sql)){ 
        echo'executed'; 
    } 
} 
?>

您可以将它们转换为这样的整数。

x="1"

y="2"

然后你应该没有问题添加它们。

答案 1 :(得分:0)

我已经破坏了您的列宽调整方法,但这里只是对文件中最后一个块进行的简单更改,应该可以执行您想要的操作。只需在EXIT = 1 WRITE = 4 LINUX_SYSCALL = 0x80 .data # data (variables) msg: .ascii "enter code here`Hello, world!\n" len = . - msg # len represents the size of the vector ## keep lines before this ## .text # section of code .global _start _start: movl $msg, %ebx movl $len, %ecx sub $2, %ecx #subtract by 2 due to \n add %ebx, %ecx movl %ebx, %edx start_for: inc %ecx dec %edx movl (%ecx), %esi movl (%edx), %edi movl %esi, %ecx movl %edi, %edx cmp %ecx, %edx je end_for end_for: ## don't change anything below ## movl $len,%edx movl $msg,%ecx movl $1,%ebx movl $WRITE,%eax # pedir write ao sistema int $LINUX_SYSCALL # chama o sistema movl $0,%ebx movl $EXIT,%eax # pedir exit ao sistema int $LINUX_SYSCALL # chama o sistema 之后替换所有内容。

# display justified column data

制作总计和付费金额的整数非常简单,找到一种显示新数据的方法还有一些工作。