Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """ 1. Read values from a 1D list, output to console: """
- # Sample 1D list
- my_list = [1, 2, 3, 4, 5]
- # Output values from the list to the console
- for value in my_list:
- print(value)
- """ 2. Read values from the console, write to a 1D list: """
- # Initialize an empty list
- my_list = []
- # Prompt the user for input and add values to the list
- while True:
- user_input = input("Enter a value (or 'exit' to quit): ")
- if user_input.lower() == 'exit':
- break
- my_list.append(user_input)
- # Display the contents of the list
- print("Contents of the list:")
- for value in my_list:
- print(value)
- """ 3. Read values from a 1D list, write to a text file: """
- # Sample 1D list
- my_list = [1, 2, 3, 4, 5]
- # Specify the name of the output text file
- output_file_name = "output.txt"
- # Open the text file for writing
- with open(output_file_name, "w") as file:
- for value in my_list:
- file.write(str(value) + "\n")
- """ 4. Read values from the console, write to a text file: """
- # Specify the name of the output text file
- output_file_name = "output.txt"
- # Open the text file for writing
- with open(output_file_name, "w") as file:
- while True:
- user_input = input("Enter a value (or 'exit' to quit): ")
- if user_input.lower() == 'exit':
- break
- file.write(user_input + "\n")
- """ 5. Read values from a text file (each value on a separate line), write to a 1D list: """
- # Specify the name of the input text file
- input_file_name = "input.txt"
- # Initialize an empty list
- my_list = []
- # Open the text file for reading
- with open(input_file_name, "r") as file:
- for line in file:
- # Remove leading/trailing whitespace and add to the list
- my_list.append(line.strip())
- # Display the contents of the list
- print("Contents of the list:")
- for value in my_list:
- print(value)
- """ 6. Read a paragraph from a text file (terminated by a single EOL character), write to a 1D list:"""
- # Specify the name of the input text file
- input_file_name = "input.txt"
- # Initialize an empty list
- my_list = []
- # Open the text file for reading
- with open(input_file_name, "r") as file:
- paragraph = file.read()
- # Split the paragraph into lines and add to the list
- my_list = paragraph.splitlines()
- # Display the contents of the list
- print("Contents of the list:")
- for value in my_list:
- print(value)
- """ 7. Read values from a 2D list, output to console: """
- python
- Copy code
- # Sample 2D list
- two_d_list = [
- [1, 2, 3],
- [4, 5, 6],
- [7, 8, 9]
- ]
- # Output values from the 2D list to the console
- for row in two_d_list:
- for value in row:
- print(value, end=" ") # Print values on the same line
- print() # Move to the next line after each row
- """ 8. Read CSV data from the console, write to a 2D list: """
- import csv
- # Initialize an empty 2D list
- two_d_list = []
- # Prompt the user for CSV input
- print("Enter CSV data (e.g., 1,2,3):")
- while True:
- user_input = input("Enter data (or 'exit' to quit): ")
- if user_input.lower() == 'exit':
- break
- # Split the input into values and append to the 2D list
- row = user_input.split(',')
- two_d_list.append(row)
- # Display the contents of the 2D list
- print("Contents of the 2D list:")
- for row in two_d_list:
- print(row)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement