当发生特定错误时,我需要从一个txt文件(accountfile.txt)获取第一行到另一个文本文件(errorfile.txt)。现在它只是添加整个txt文件而不仅仅是第一行< / p>
def get_account():
with open(os.path.join(os.path.join(os.getcwd(), 'settings'), 'accountfile.txt'), 'r') as file:
account = [i.strip() for i in file]
if account:
return account
def add_error():
account = get_account()
with open(os.path.join(os.path.join(os.getcwd(), 'settings'), 'errorfile.txt'), 'w') as file:
file.write('{}:error\n'.format(erroraccount)
我需要在def add_error()部分更改什么?
答案 0 :(得分:0)
使用if GalleryData.where((:gallery_origin => :id) & (:file_name => params[:file])).blank?
...
,您将获得文件中的所有行。你想要的只是第一个。
使用此固定代码:
[i.strip() for i in file]
或者更好,请使用:
def get_account():
with open(os.path.join(os.path.join(os.getcwd(), 'settings'), 'accountfile.txt'), 'r') as file:
account = [i.strip() for i in file][0]
if account:
return account
def get_account():
with open(os.path.join(os.path.join(os.getcwd(), 'settings'), 'accountfile.txt'), 'r') as file:
account = file.readline()
if account:
return account
读取第一行,这就是您所需要的。
以下代码从file.readline()
读取并删除第一行,然后将此行写入accountfile
:
errorfile
我无法判断这是否是最好的&#34;方式,但只要with open('/Users/ammar/Desktop/x.txt', 'r') as f_acc:
lines = f_acc.readlines()
with open('/Users/ammar/Desktop/x.txt', 'w') as f_acc, \
open ('/Users/ammar/Desktop/xx.txt', 'w') as f_err:
f_acc.write(''.join(lines[1:]))
f_err.write('{}:error\n'.format(lines[0].strip()))
包含文本,它就会起作用。
答案 1 :(得分:0)
方法SELECT
A.BUSINESS_UNIT
,A.ASSET_ID
,A.BOOK
,A.FISCAL_YEAR
,A.ACCOUNTING_PERIOD
,A.CATEGORY
,A.ACCOUNT_FA AS ACCOUNT
,A.CURRENCY_CD
,CASE WHEN A.COST_ADD <> 0 THEN 'COST_ADD' ELSE 'ACCUM_DEPR_ADD' END AS MOT
,CASE WHEN A.COST_ADD <> 0 THEN A.COST_ADD ELSE A.ACCUM_DEPR_ADD END AS AMOUNT
FROM SYSADM.PS_DEPR_RPT A
WHERE A.BOOK IN ('CORP-BASE','CORPORATE');
从文件中读取整行。
readline()