473,325 Members | 2,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

How do I import a list or array from a text file, manipulate it, then output it?

I have written the following code that takes a list called migList, multiplies the strings by 5, then outputs it to a text file called migList.txt
Expand|Select|Wrap|Line Numbers
  1. #define a list called migList
  2. migList = ['cat', 'dog', 'car', '22']
  3.  
  4. #now add 5 to every unit in migList
  5.  
  6. for i in range(len(migList)):
  7.                migList[i] = migList[i]*5
  8.  
  9. fn = "migList.txt"
  10. f = open(fn,'w')
  11. f.write("\n".join(migList))
  12. f.close()
  13.  
Question 1: Now assuming that the list is in a text file, how do I parse in those values into a list that I can then manipulate and save to a text file?
The only code I can come up with is the following:
Expand|Select|Wrap|Line Numbers
  1. f = open('migList.txt', 'r')#opens the file and sets it to read
  2.  
After that, I'm stuck. I know I want to create a new list and then iterate over the items in the text file but I'm not quite sure how.

Question 2: When I use integers in the list and try to save using the code above, I get the following error.
Expand|Select|Wrap|Line Numbers
  1. TypeError: sequence item 0: expected string, int found
I believe I need to be writing a string to the file, and the command s = str(value) can be used but I'm not sure how to use that.
I tried the following code which seemed to work.
Expand|Select|Wrap|Line Numbers
  1. migList[i] = str(migList[i])
but is there a different way to do it? (I assume this is where pickle comes into play).
Again, thanks for your help, as my eventual goal is to learn enough about manipulating data in python to be able to manipulate some traffic data.
Dec 16 '10 #1

✓ answered by dwblas

readlines() will read the file into a list (assuming your file is not gigabytes in size). An overview of read and write.
Expand|Select|Wrap|Line Numbers
  1. f = open('migList.txt', 'r').readlines()
  2. for rec in f:
  3.     print f 

2 2257
dwblas
626 Expert 512MB
readlines() will read the file into a list (assuming your file is not gigabytes in size). An overview of read and write.
Expand|Select|Wrap|Line Numbers
  1. f = open('migList.txt', 'r').readlines()
  2. for rec in f:
  3.     print f 
Dec 16 '10 #2
That's a great first step but if I'm reading a simple file, i.e.
1
2
25
I get a list generated like so:
['1\n', '2\n', '25\n']
In order to use these values, I guess I have to clean up the list?
Dec 17 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: John M. Lembo | last post by:
I am using Perl to parse a text file and output to another file. The text file has data on Unix virtual memory (vmstat) and I want to delete lines that I don't want and output lines that I want to...
1
by: Sven | last post by:
Hello, I am receiving a text file that is produced from a mainframe that is out of my control. I am attempting to find a (hopefully clean) way to import it into a SQL Server database in an...
1
by: Service Tech | last post by:
I have created a DTS that imports a text file to by data table. I get errors when ever I run this since there are fields in the table that are numeric. I understand that I need to create an activeX...
3
by: Yellowbird | last post by:
I have an HTML form with a pre-built table containing "standard" column headings (for example, First Name, Last Name, SSN, Hours, etc.). I want a user to be able to enter values into the form and...
1
by: lantamer | last post by:
Hi I need to make script that creates drop-down list from text file (new line in text file – new line in drop-down list). Somebody told me that I should use Ajax and XMLHttpRequest for that, so I...
3
by: shil | last post by:
Hi, I'm trying to find a right way the data from a TAB delimited UTF-8 text file into SQL database. My text file has Unicode characters like ö, é. I'm trying to code this in vb.net. Can any one...
7
by: nitinloml | last post by:
well i m having a text file which contain time,user name & id, now if i want to modify the time without affecting the id & user name is it possible? or how i delete that list & enter a new...
1
by: CAM123 | last post by:
I have added: <br><xsl:value-of select="Line" /></br> to my XSLT stylesheet to get a line per repeating block. When I view the output as XML it looks perfect - one line per block. However...
13
by: jhamb | last post by:
Hi, This code is in Perl (just a trial, not tested) to parse a text file and output to another file. It is used to delete lines that are not required and output lines that the user wants, to a new...
0
by: ahmed222too | last post by:
i have a text file contains data (tab delimited), i want to import these data into access table. i want to do this task using visual basic code not by access wizard
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.