2016-11-29 2 views
1

プログラムを実行すると、空白のCSVファイルに各製品間に1行のギャップがあるようです。あなたはどうやってそのギャップを取り除くのですか? (最初の写真に示されている)CSVファイルのギャップをどのように取り除きますか?

This is what happens when I fully run my code

This is my original CSV file that contains all the product information. Row A is the GTIN-8 code, row B is the product name, C is the current stock level, D is the re-order stock level, and E is the target stock level (Just to clarify)

これは私のコードです:

import csv 
redo='yes' 

receipt=open('receipt.txt', 'wt') 

stock=open('Stock_.csv', 'rt') 
stock_read=csv.reader(stock) 

blank_csv=open('Blank_csv_.csv', 'wt') 
blank_csv_write=csv.writer(blank_csv) 


clothes=(input('\nPlease enter the GTIN-8 code of what you want to purchase: ')) 
quantity=int(input('\nPlease enter the amount of this product you want to buy: ')) 

for row in stock_read: 
    GTIN=row[0] 
    product=row[1] 
    current=row[2] 
    re_order=row[3] 
    target=row[4] 

    if clothes==GTIN: 
     current=int(current)-quantity 
    blank_csv_write.writerows([[GTIN,product,current,re_order,target]]) 

stock.close() 
blank_csv.close() 

reorder_receipt=open('receipt.txt', 'wt') 

blank_csv2=open('Blank_csv_.csv', 'rt') 
blank_csv_read2=csv.reader(blank_csv2) 

stock_check=input('Press \"ENTER\" if you want to check the current stock leavels: ') 

if stock_check=='': 
    for row in blank_csv_read2: 
     for field in row: 

      GTIN=row[0] 
      product=row[1] 
      current=int(row[2]) 
      re_order=int(row[3]) 
      target=int(row[4]) 
      if current<=re_order: 
       re_stock=target-current 
       reorder_receipt.write(GTIN+' '+product+' '+str(current)+' '+str(re_order)+' '+str(target)+' '+str(re_stock)+'\n') 

blank_csv2.close() 
reorder_receipt.close() 

は、事前にありがとう!!!!

+0

writerowsの代わりにblank_csv_write.writerow([GTIN、product、current、re_order、target])を使用すると問題が解決しますか? (私はブラケットのペアを削除したことに注意してください) –

答えて

1

これはTHIS号の重複しているようです。私はそこの答えがあなたの問題を解決するのに役立つと信じています。ヒント:改行= ''

0

私はあなたのコードを詳細に調べなかったが、一見すると、データの各行の間の余分な行を削除したい場合、改行文字 '\ n'をreorder_receipt.write()関数呼び出しです。言い換えれば

、変更:

reorder_receipt.write(GTIN+' '+product+ ... +str(re_stock)+'\n')

をする:

reorder_receipt.write(GTIN+' '+product+ ... +str(re_stock))

0

私は問題がtarget=row[4]であると思います。 "target"文字列が余分な\ nを得ている可能性があります。だから、それを取り除くことで問題が解決するかもしれない。 target.strip()

関連する問題