Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9252381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:55:50+00:00 2026-06-18T10:55:50+00:00

I am a newbie trying to teach myself Python. I have a file with

  • 0

I am a newbie trying to teach myself Python. I have a file with a bunch of numbers in it and I want to “import” them into a python list as integers (or at least that’s what I think I want to do). I seem to be having a problem, but I don’t understand what it is. Here is some detailed information on my problem and the code I’ve tried:

I have a DNA sequence (e.g. a string of ~ 150,000 letters) and I would like to have python go to a certain position in that string and then print the 150 letters to the left of that position, the letter at that position surrounded by square brackets, and then 150 letters to the right of that position. I need to do this for >100 positions in the string. I have a list of these positions in a separate file. I have figured out that Biopython has an object that will handle the very long string for me, and if I tell python what position I want (e.g. assign it by hand), I can slice this string and get the correct output. Now I want to be able to import my target positions from this other file and then have python iteratively go through that list and print the output to another file. The first part is where I am having some trouble.

I have tried the input file in several different formats. One like this:

500,1000,15000

And another one like this (all positions on separate lines):

500

1000

15000

Based on some other posts I read I have tried several things. Here is one:

from Bio import SeqIO
import csv

with open('Results.fa', 'a') as f1:
    Reference = SeqIO.read("GEO5FinalAssembly2SC.fa", "fasta") # Biopython
    DataFile = open('TestFile.csv', 'r')
    DataReader = csv.reader(DataFile)
    SNP = []
    for row in DataReader:
        SNP.append(row)
    for i in SNP:
        IA=i-151  #Creating the intervals
        IB=i-1
        JA=i+1
        JB=i+151
        Fragment = Reference.seq[IA:IB] + "[" + Reference.seq[i] + "]" + Reference.seq[JA:JB]
        F = str(Fragment)      #Need to turn Fragment into a string that can be written
        header = ">MINT_SNP" + str(i) + "\n"
        f1.write(header)
        f1.write(F)
        f1.write("\n")

This returns the error:

Traceback (most recent call last):
  File "./ReferenceSplitter3.py", line 15, in <module>
    IA=i-151  #Creating the intervals
TypeError: unsupported operand type(s) for -: 'list' and 'int'

I also tried this:

from Bio import SeqIO
import csv

with open('Results.fa', 'a') as f1:
    Reference = SeqIO.read("GEO5FinalAssembly2SC.fa", "fasta")
    with open('TestFile.txt', 'r') as Input:
        rows = csv.reader(Input, quoting=csv.QUOTE_NONNUMERIC)
        SNP = [[item for number, item in enumerate(row)] for row in rows]       
    for i in SNP:
        IA=i-151  #Creating the intervals
        IB=i-1
        JA=i+1
        JB=i+151
        Fragment = Reference.seq[IA:IB] + "[" + Reference.seq[i] + "]" + Reference.seq[JA:JB]
        F = str(Fragment)      #Need to turn Fragment into a string that can be written
        header = ">SNP" + str(i) + "\n"
        f1.write(header)
        f1.write(F)
        f1.write("\n")

This gives a similar error:

Traceback (most recent call last):
  File "./ReferenceSplitter4.py", line 13, in <module>
    IA=i-151  #Creating the intervals
TypeError: unsupported operand type(s) for -: 'list' and 'int'

However, when I define a list of integers myself like this SNP = (500,1000,1500) it seems to work just fine. I wonder if I am missing some fundamental python concept here. Sorry if this is a really basic question, but any suggestions would be much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T10:55:52+00:00Added an answer on June 18, 2026 at 10:55 am

    For the input where they are all on the same line separated by commas (500,1000,10000), you can read it in with:

    SNP = next(DataReader)
    

    For the input where they are each on a different line, do:

    SNP = []
    for row in DataReader:
        SNP.append(row[0])
    

    Either will set SNP to a list of numbers, like [500, 1000, 15000], than you can then iterate over.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Super newbie question here. I'm trying to teach myself Web Forms, so I'm experimenting
Newbie question here. I'm trying to teach myself, and sometimes the solo route can
As a newbie I am trying to run a PHP file and then an
I'm a newbie and was trying something in python 2.7.2 with Numpy which wasn't
Complete rails newbie trying to get started. I have two classes, Ingredient, and Unit.
I'm a total newbie trying to use Python to analyze my company's log files.
I am a QT newbie and trying to play around with Apps. I have
I am a newbie trying out SSH. The scenario is I have 2 machines
I'm a newbie programmer trying to jump in to Python by building a script
Hello I'm a newbie trying to use django to register some users, I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.