Question

Help with Python Example of scsv file : TOXIC;U;Q;120;4;5;289;2;19;2;123;25;7;8;0.23;89.98745 APPLE;V;B;89;2;3;344;6;8;5;450;55;7;2;0.37;34.23205 Convert this format to the common...

Help with Python

Example of scsv file :

TOXIC;U;Q;120;4;5;289;2;19;2;123;25;7;8;0.23;89.98745

APPLE;V;B;89;2;3;344;6;8;5;450;55;7;2;0.37;34.23205

Convert this format to the common csv file format

Use semicolons for those values that have commas in them.

  1. Define the function named organize_table. It has 2 parameters a filename to be read (the scsv file) and a filename to be written (the csv file). Name them raw_file and organized_file.

  2. Open, read, and parse the scsv file (i.e. raw_file)

  3. Define the same contents to organized_file but separate the values (columns) with commas.

  4. For those values that have commas in them, replace the commas with semicolons.

  5. Be sure to end each line (except the last line) with a newline.

  6. Return True from the function. Return a boolean indicating success or failure

  7. Please don't use any libraries

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#code


#required method
def organize_table(raw_file, organized_file):
    #opening files inside a try-except block
    try:
        #opening raw_file in read mode, using the same variable name
        raw_file=open(raw_file,'r')
        #opening organized_file in write mode
        organized_file=open(organized_file,'w')
        #creating a variable to store the updated text
        formatted_text=''
        #looping through each line in raw_file
        for line in raw_file:
            #removing heading/trailing newline/spaces from line, splitting by semi colon
            fields=line.strip().split(';')
            #looping through each element in split array
            for i in range(len(fields)):
                #if current element contains comma, replacing with semi colon
                if ',' in fields[i]:
                    fields[i]=fields[i].replace(',',';')
            #combining fields array using comma as separator, appending to formatted_text
            #followed by a newline
            formatted_text+=','.join(fields)+'\n'
        #writing to organized_file after removing trailing newline
        organized_file.write(formatted_text.strip())
        #closing file(s), saving changes
        organized_file.close()
        raw_file.close()
        return True #success
    except:
        return False #failed

Add a comment
Know the answer?
Add Answer to:
Help with Python Example of scsv file : TOXIC;U;Q;120;4;5;289;2;19;2;123;25;7;8;0.23;89.98745 APPLE;V;B;89;2;3;344;6;8;5;450;55;7;2;0.37;34.23205 Convert this format to the common...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PLEASE DO IT IN PYTHON, THANK YOU! CSV file: This is a file containing plain text...

    PLEASE DO IT IN PYTHON, THANK YOU! CSV file: This is a file containing plain text where each line in the file represents one record of information, and each piece of info in the record is separated by a single comma. Luckily the values won't ever contain commas themselves for this project. But they might start or end with non visible characters (e.g. space, tab). Thus, you must remove the leading and trailing spaces when you store the data in...

  • Define a Python function named borough_count that has one parameter. The parameter is a string representing...

    Define a Python function named borough_count that has one parameter. The parameter is a string representing the name of a CSV file. The CSV file is a subset of NYC's dataset of all film permits issued since April 2016. Each row in the CSV file has the format: Event Id, Police Precinct(s), Event Type, Borough, Category Your function must return a dictionary. The keys of this dictionary will be the boroughs read in from the file (boroughs are at index...

  • In Unix/Linux, input and output are treated as files and referenced by the operating system using file descriptors. When you open a shell session, for example, three file descriptors are in use: 0 st...

    In Unix/Linux, input and output are treated as files and referenced by the operating system using file descriptors. When you open a shell session, for example, three file descriptors are in use: 0 standard input (stdin) 1 standard output (stdout) 2 standard error (stderr) By default, the command interpreter (shell) reads keyboard input from file descriptor 0 (stdin) and writes output to file descriptor 1 (stdout), which appears on the screen. As you explored in Lab 2, input/output can be...

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

  • PYTHON Hello can someone create the following functions? I'm not sure how to start on them....

    PYTHON Hello can someone create the following functions? I'm not sure how to start on them. makeDictionary Define a function named makeDictionary with two parameters. The first argument passed the function must be a list of strings to be used as keys, while the second is a list of the same length containing values (of some type). This function must return a dictionary consisting of the keys paired with the corresponding values. For example, makeDictionary(['apno','value'],['REP-12','450']) must return this dictionary: {'apno':...

  • Help needed with Python 3: Dictionaries and Sets. The problem is one that asks the user to create...

    Help needed with Python 3: Dictionaries and Sets. The problem is one that asks the user to create a completed program that prompts the user for the name of a data file, and reads the contents of that data file, then creates variables in a form suitable for computing the answer to some questions. The format should be in the same manner as the attached .py file. All subsequent assignment details (.py and .csv files) can be found at this...

  • According to Wikipedia , a comma-separated values (CSV) file is a delimited text file that uses...

    According to Wikipedia , a comma-separated values (CSV) file is a delimited text file that uses a comma to separate values. A CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format. A company has text data that is not...

  • Write a Python program named aIP.py which will read data from a file named wireShark.txt and...

    Write a Python program named aIP.py which will read data from a file named wireShark.txt and extract all the pairs of source and destination ip addresses and output them in pairs to another file called IPAddresses.txt , one per line, listing source and destination. Example of output: Source                      Destination 192.168.1.180         239.255.255.250 Detailed Requirements: You will read from a file called wireShark.txt which, to avoid problems with finding paths, will be located in the same directory as your code You will...

  • Python coding exercise: please include comments Goal #1: import financial data given into your pr...

    Python coding exercise: please include comments Goal #1: import financial data given into your program provided to you as a CSV formatted text file Use the following data for testing (the following is only a sample of the data; there are over 4000 rows of data): *Note: The data you will read in is linear by date (but, non-contiguous due to holidays and weekends,) reflecting a timeline of stock performance in chronological order; however your program should run through the...

  • Python coding exercise: please include comments Goal #1: import financial data given into your program provided...

    Python coding exercise: please include comments Goal #1: import financial data given into your program provided to you as a CSV formatted text file Use the following data for testing (the following is only a sample of the data; there are over 4000 rows of data): *Note: The data you will read in is linear by date (but, non-contiguous due to holidays and weekends,) reflecting a timeline of stock performance in chronological order; however your program should run through the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT