Question

Consider python please for this problem: I did a python program to read the first 20...

Consider python please for this problem:

I did a python program to read the first 20 lines from a file and here is the code

holder = 20
lines = 3
file = "123456.pssm"
_file = ".txt"

while 1:
   
    out_file = input("[OUTPUT] - Please enter the name of file: ") + _file

   
    if (not _file):
        print("Filename not specified - Please Try Again\n")
        continue

    break

with open(_file, "a") as e:
    with open(file, "r") as f:
        for num, line in enumerate(f, 1):
           
            if (line < num < holder + 1):
               
                print(line.strip())
                e.write(line.strip() + "\n")

print("Information saved to {}".format(_file))

What i need is change it to make it take the first 20 columns not rows can you help please

here is the file i need to take the 20 coulmns from

https://drive.google.com/file/d/1GWv7-jmZKwH-ssSWVHNgecHvmm29UeTQ/view?usp=sharing

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

Solution :-

Screenshot of the code...

Code in the text format (Kindly take care of indentation , refer screenshot if needed ... )

holder = 20
lines = 3
file = "123456.pssm"
_file = ".txt"

while 1:
out_file = input("[OUTPUT] - Please enter the name of file: ") + _file
if (not _file):
print("Filename not specified - Please Try Again\n")
continue
break
with open(str(out_file), "w") as e:
with open(file, "r") as f:
for line in f:
k = line.strip().split()
if len(k) >= 20:
print(line.strip())
for i in range(holder):
e.write( k[i] + " ")
e.write("\n")

print("Information saved to {}".format(out_file))

Screenshot of the file written (first 20 columns ) using python code... (Run the code using python filename.py)

Add a comment
Know the answer?
Add Answer to:
Consider python please for this problem: I did a python program to read the first 20...
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
  • Consider python please for this problem: I did a python program to read the first 20 lines from a...

    Consider python please for this problem: I did a python program to read the first 20 lines from a file and here is the code holder = 20 lines = 3 file = "123456.pssm" _file = ".txt" while 1:         out_file = input("[OUTPUT] - Please enter the name of file: ") + _file         if (not _file):         print("Filename not specified - Please Try Again\n")         continue     break with open(_file, "a") as e:     with open(file, "r") as f:         for num, line in enumerate(f, 1):                        ...

  • use python IDEL Please highlight the answer Problem 3 Files. Read the following code and answer...

    use python IDEL Please highlight the answer Problem 3 Files. Read the following code and answer the next five questions about it. The contents of the files list 1.txt and list2.txt are the following (given side by side): list 1. txt content: list2.txt content 2 apples 2 apples 3 oranges 1 loaf of bread 2 eggs 2 eggs 1 cereal box # the program code starts here filename1 = "list 1.txt" filename2 - "list2.txt" filename3 - "listdiff.txt" with open (filename1,"r")...

  • I cant get this python program to read all the unique words in a file. It...

    I cant get this python program to read all the unique words in a file. It can only read the number of the unique words in a line. import re import string from collections import Counter # opens user inputted filename ".txt" and (w+) makes new and writes def main(): textname = input("Enter the file to search: ") fh = open(textname, 'r', encoding='utf-8' ) linecount = 0 wordcount = 0 count = {} print("Sumary of the", fh) for line in...

  • PYTHON PROGRAMMING: I have this program right now where it allows users to choose from a...

    PYTHON PROGRAMMING: I have this program right now where it allows users to choose from a category(pulling from the file). Then it will print the University or people from that text file. What I want to do next on my code is for users to search for a specific string from that file and it will display both the University and People that have that matching string. It can be the whole word or part of the string from that...

  • Hello, This is what i wrote: def squareEach(nums): answer = [] for num in nums: answer.append(num*num)...

    Hello, This is what i wrote: def squareEach(nums): answer = [] for num in nums: answer.append(num*num) return answer def sumList(nums): answer = 0 for num in nums: answer += num return answer def toNumbers(nums): new_nums = [] for i in nums: new_nums.append(int(i)) return new_nums def main(): fileName = input("What file are the numbers in? ") sum = 0 with open(fileName, 'r') as infile: for line in infile: nums.append(line) print(nums) nums = toNumbers(line) print(nums) squares = squareEach(nums) print(nums) sum += sumList(squares)...

  • Python 12.10 LAB: Sorting TV Shows (dictionaries and lists) Write a program that first reads in...

    Python 12.10 LAB: Sorting TV Shows (dictionaries and lists) Write a program that first reads in the name of an input file and then reads the input file using the file.readlines() method. The input file contains an unsorted list of number of seasons followed by the corresponding TV show. Your program should put the contents of the input file into a dictionary where the number of seasons are the keys, and a list of TV shows are the values (since...

  • I am having a little trouble with my Python3 code today, I am not sure what...

    I am having a little trouble with my Python3 code today, I am not sure what I am doing wrong. Here are the instructions: and here is my code: update: I have seen I did not close x in sumFile and I am still only getting a 2/10 on the grader. any help appreciated. Lab-8 For today's lab you going to write six functions. Each function will perform reading and/or write to a file Note: In zybooks much like on...

  • Hi. It's a python and I got an error below comment import numpy as np arr=np.genfromtxt("/Volumes/Samsung...

    Hi. It's a python and I got an error below comment import numpy as np arr=np.genfromtxt("/Volumes/Samsung SSD 860 EVO 500GB Media/Download/primenumbers.txt", dtype=int) arr=arr.reshape(-1,1) arr.shape def find_cat(x): if x<= 300: return '<=300' elif x <= 600: return '<=600' else: return '<=1000' arr2 = np.apply_along_axis(find_cat, axis=1, arr=arr) arr2 = arr2.reshape(-1,1) arr3 = np.hstack((arr, arr2)) arr_300 = np.array((col[0] for col in arr3 if col[i]=='<=300'), dtype=int) arr_300 arr_300.shape count_300=len(arr_300) count_300 avg_300=round(np.mean(arr_300, 2) avg_300 print("Number of items in category \ "<=300\"= (one), and average of...

  • Need assistance with syntax errors two python scripts: tester: cat testdata2.txt | python kmeansMapper.py | sort...

    Need assistance with syntax errors two python scripts: tester: cat testdata2.txt | python kmeansMapper.py | sort | python kmeansReducer.py #kmeansReducer.py #!/usr/bin/python import sys currId = None # this is the "current" key currXs = [] currYs = [] id = None # The input comes from standard input (line by line) for line in sys.stdin:     line = line.strip()     ln = line.split('\t')     id = ln[0] if currId == id:     currXs.append(float(ln[1]))     currYs.append(float(ln[2])) else:   if currId:     #calculate center     centerX = sum(currXs)/len(currXs)     centerY =...

  • I am working on Exercise 5.8 from Fundamentals of Python 2nd edition. Do you have a...

    I am working on Exercise 5.8 from Fundamentals of Python 2nd edition. Do you have a solution for this item? Thank you. Here is the code I have. I just need 1 instance of each to show in my output. should look like: AM 3 I 3 SAM 3 Thank you... # Put your code here #fileName = input("Enter the file name: ") fileName = "example.txt" #fileName = "kgtest.txt" inputFile = open(fileName, 'r') text = inputFile.read() #print(text) words = text.split()...

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