.txt file being created, but file is blank without invert_dictionary contents (python)

Task: Write a program to read dictionary items from a file and then write the inverted dictionary to a file.

Problem: The new output_file is created, but it’s created blank. I’ve read a few forums and tried to make some changes (like adding a .close(), but this seems to have mixed reviews when already using with), so I’ve taken that bit out. Unsure where to go next to improve my code and get the right result! New to coding.

  • tried adding .close() to the code, then removed
  • tried modifying the contents of input_file, adding curly brackets, making list items strings, etc. — unsure what is correct here, does it have to be a properly formatted list?
  • added if line.count section with continue

Current contents of maindictionary.txt

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
'Art': ['Pocahontas', 'Jasmine', 'Tiana'],
'Computer Science': ['Rapunzel', 'Cinderella', 'Jasmine', 'Moana'],
'English': ['Aurora', 'Rapunzel', 'Mulan', 'Tiana', 'Pocahontas'],
'Geography': ['Belle', 'Cinderella', 'Ariel', 'Thumbelina'],
'History': ['Cinderella', 'Ariel', 'Thumbelina', 'Aurora'],
'Physical Education': ['Aurora', 'Mulan', 'Belle', 'Ariel'],
'Mathematics': ['Jasmine', 'Aurora', 'Rapunzel', 'Moana'],
'Science': ['Moana', 'Tiana', 'Pocahontas', 'Thumbelina', 'Belle']
}
</code>
<code>{ 'Art': ['Pocahontas', 'Jasmine', 'Tiana'], 'Computer Science': ['Rapunzel', 'Cinderella', 'Jasmine', 'Moana'], 'English': ['Aurora', 'Rapunzel', 'Mulan', 'Tiana', 'Pocahontas'], 'Geography': ['Belle', 'Cinderella', 'Ariel', 'Thumbelina'], 'History': ['Cinderella', 'Ariel', 'Thumbelina', 'Aurora'], 'Physical Education': ['Aurora', 'Mulan', 'Belle', 'Ariel'], 'Mathematics': ['Jasmine', 'Aurora', 'Rapunzel', 'Moana'], 'Science': ['Moana', 'Tiana', 'Pocahontas', 'Thumbelina', 'Belle'] } </code>
{
   'Art': ['Pocahontas', 'Jasmine', 'Tiana'],
   'Computer Science': ['Rapunzel', 'Cinderella', 'Jasmine', 'Moana'],
   'English': ['Aurora', 'Rapunzel', 'Mulan', 'Tiana', 'Pocahontas'],
   'Geography': ['Belle', 'Cinderella', 'Ariel', 'Thumbelina'],
   'History': ['Cinderella', 'Ariel', 'Thumbelina', 'Aurora'],
   'Physical Education': ['Aurora', 'Mulan', 'Belle', 'Ariel'],
   'Mathematics': ['Jasmine', 'Aurora', 'Rapunzel', 'Moana'],
   'Science': ['Moana', 'Tiana', 'Pocahontas', 'Thumbelina', 'Belle']
}

Current code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> # indicate the file paths for the two files
input_file = "/Python/maindictionary.txt"
output_file = "/Python/inverteddictionary.txt"
# read the original dictionary from a file
def read_dictionary(input_file):
main_dictionary = {}
with open(input_file, 'r') as file:
for line in file:
if line.count(',') != 1:
continue
k, v = line.strip().split(',')
main_dictionary[k.strip()] = v.strip()
return main_dictionary
# invert the dictionary
def invert_dictionary(main_dictionary):
inverted_dictionary = {}
for key, value in main_dictionary.items():
if value not in inverted_dictionary:
inverted_dictionary[value] = [key]
else:
inverted_dictionary[value].append(key)
return inverted_dictionary
# write the inverted dictionary to a different file
def write_inverted_dictionary(inverted_dictionary, output_file):
with open(output_file, 'a') as file:
for k, v in inverted_dictionary.items():
file.write(f"{k}: {', '.join(v)}n")
# read the main dictionary from the file
main_dictionary = read_dictionary(input_file)
# invert the main dictionary, like in programming assignment 7
inverted_dictionary = invert_dictionary(main_dictionary)
# write the inverted dictionary to a new file
write_inverted_dictionary(inverted_dictionary, output_file)
</code>
<code> # indicate the file paths for the two files input_file = "/Python/maindictionary.txt" output_file = "/Python/inverteddictionary.txt" # read the original dictionary from a file def read_dictionary(input_file): main_dictionary = {} with open(input_file, 'r') as file: for line in file: if line.count(',') != 1: continue k, v = line.strip().split(',') main_dictionary[k.strip()] = v.strip() return main_dictionary # invert the dictionary def invert_dictionary(main_dictionary): inverted_dictionary = {} for key, value in main_dictionary.items(): if value not in inverted_dictionary: inverted_dictionary[value] = [key] else: inverted_dictionary[value].append(key) return inverted_dictionary # write the inverted dictionary to a different file def write_inverted_dictionary(inverted_dictionary, output_file): with open(output_file, 'a') as file: for k, v in inverted_dictionary.items(): file.write(f"{k}: {', '.join(v)}n") # read the main dictionary from the file main_dictionary = read_dictionary(input_file) # invert the main dictionary, like in programming assignment 7 inverted_dictionary = invert_dictionary(main_dictionary) # write the inverted dictionary to a new file write_inverted_dictionary(inverted_dictionary, output_file) </code>
    # indicate the file paths for the two files
    input_file = "/Python/maindictionary.txt"
    output_file = "/Python/inverteddictionary.txt"

    # read the original dictionary from a file
    def read_dictionary(input_file):
        main_dictionary = {}
        with open(input_file, 'r') as file:
            for line in file:
                if line.count(',') != 1:
                    continue
                k, v = line.strip().split(',')
                main_dictionary[k.strip()] = v.strip()
        return main_dictionary

    # invert the dictionary
    def invert_dictionary(main_dictionary):
        inverted_dictionary = {}
        for key, value in main_dictionary.items():
            if value not in inverted_dictionary:
                inverted_dictionary[value] = [key]
            else:
                inverted_dictionary[value].append(key)
        return inverted_dictionary

    # write the inverted dictionary to a different file
    def write_inverted_dictionary(inverted_dictionary, output_file):
        with open(output_file, 'a') as file:
            for k, v in inverted_dictionary.items():
                file.write(f"{k}: {', '.join(v)}n")

    # read the main dictionary from the file
    main_dictionary = read_dictionary(input_file)

    # invert the main dictionary, like in programming assignment 7
    inverted_dictionary = invert_dictionary(main_dictionary)

    # write the inverted dictionary to a new file
    write_inverted_dictionary(inverted_dictionary, output_file)

New contributor

Sarah is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật