Как да се избегне новата линия?
Здравейте, въпросът ми е относно относно четене на файлове, по-конкретно от последната задача в края на 3-та лекция за променяне на цените в каталог.
Ето кодът ми:
"""
Change prices for products
of different kind with 75% or more.
"""
import random
# Dictionary with a mapping of how much we should change
# the prices of different clothing - it will be random between 75% and 80%
CATALOG_PRICES_SAMPLE = 'catalog_sample.csv'
CATALOG_PRICES_FULL = 'catalog_full.csv'
CATALOG_PRICES_FULL_MODIFIED = 'catalog_full_modified.csv'
dictionary_increase_in_prices = {}
index_price = 6
index_clothing_type = 4
with open(CATALOG_PRICES_FULL, 'r') as file_to_read:
with open(CATALOG_PRICES_FULL_MODIFIED,'w') as file_to_write:
for line_read in file_to_read:
line_read = line_read.strip()
line_read = line_read.split(',')
line_read[6] = float(line_read[6])
clothing_type = line_read[index_clothing_type]
if clothing_type not in dictionary_increase_in_prices:
dictionary_increase_in_prices.setdefault(clothing_type,random.uniform(1.75,1.80))
line_read[6] = line_read[6]*dictionary_increase_in_prices[clothing_type]
line_read[6] = str(line_read[6])
line_to_write = (',').join(line_read)
file_to_write.write(line_to_write + '\n')
Въпросът ми е - как мога да избегна да пиша нова линия в края на новия файл, без да зареждам всичко в паметта? Знам, че мога да заредя новия файл в лист и след това да направя лист = лист[:-1] и после пак да го запиша, но има ли по - добър подход от гледна точка на памет и време?