473,396 Members | 1,774 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,396 software developers and data experts.

how to read to and write from a file

9
Reading from a file and writing to a different file. Any guide on how to initialise the read and write functions?
Dec 7 '13 #1
13 1337
mar12
9
How do i initialise the r and w command?
Dec 7 '13 #2
Luuk
1,047 Expert 1GB
i started reading here:
http://docs.python.org/2/tutorial/inputoutput.html


and came up with:
Expand|Select|Wrap|Line Numbers
  1. luuk@opensuse:~/temp> cat abcd
  2. a
  3. b
  4. c
  5. d
  6.  
  7. luuk@opensuse:~/temp> cat abcd.py
  8. #!/usr/bin/python
  9.  
  10. f = open('abcd','r')
  11. for line in f:
  12.   print ord(line[0:1])
  13.  
  14. luuk@opensuse:~/temp> ./abcd.py
  15. 97
  16. 98
  17. 99
  18. 100
  19. 10
  20. luuk@opensuse:~/temp>
  21.  
Dec 7 '13 #3
Luuk
1,047 Expert 1GB
but slightly better:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2.  
  3. f = open('abcd','r')
  4. c = True
  5. while c:
  6.         c = f.read(1)
  7.         if c>' ':
  8.                 print ord(c)
Dec 7 '13 #4
mar12
9
Hi thanks, i created a text file with the same characters as my input but the code "print ord(line[0:1])" is not working. I know ord is for ascii.
Dec 7 '13 #5
mar12
9
Reading from a file and writing to a different file. Any guide on how to initialise the read and write functions?
Dec 7 '13 #6
Luuk
1,047 Expert 1GB
If your input file contains this:
Expand|Select|Wrap|Line Numbers
  1. 1 34
  2. 12 4
  3. 123 
  4.  
can you tell, what the expected output should be?

Because you are talking about 'product', and as far as my English goes, the product of 2 and 3 is (2x3=) 6
But i only see additions in your 'code-so far'...
Dec 7 '13 #7
mar12
9
Reading from a file and writing to a different file. Any guide on how to initialise the read and write functions?
Dec 7 '13 #8
Luuk
1,047 Expert 1GB
I asked a question, and i did not get an answer.....

Why do you think i asked that question?

Just so you could post your code again, without specifying what kind of error you have? and on what line the error is?

rule1: please use '[code]' tags around your code
rule2: only post code if its different from previous post ;)
rule3: answer a question when asked, how else can you be helped?
rule4: i do not make rules here........

But, to answer your question,.....

Your code gives an error, on what line, and what error?
Expand|Select|Wrap|Line Numbers
  1.     wordlist = line.split()
  2.            ^
  3. IndentationError: expected an indented block
  4.  
It it's not this error, than please use '[code]' tags to post your code..... ;)
Dec 7 '13 #9
mar12
9
not outputting the result in another file named b.txt
Dec 7 '13 #10
mar12
9
not outputting the result in b.txt
Dec 7 '13 #11
Luuk
1,047 Expert 1GB
maybe because you are only outputting it to screen:
Expand|Select|Wrap|Line Numbers
  1. print("The product is", product)
Dec 7 '13 #12
mar12
9
Here is the problem:
y.write(line) outputs the result as characters in b.txt
but i need the result as integer value.
Dec 7 '13 #13
Luuk
1,047 Expert 1GB
"y.write(line) outputs the result as characters in b.txt"
what is y ??
Dec 7 '13 #14

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

Similar topics

22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
2
by: blip | last post by:
Is this acceptable ? It seems too easy and too simple... #include<iostream> #include<fstream> #include<cstdlib> #include<string> struct Person{ char name ; int age ;
1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
16
by: ben beroukhim | last post by:
I have huge number of legacy code which use standard files functions. I would like to pass a memory pointer rather than a FILE pointer. I am trying to use FILEs in the code to refer to memory...
8
by: Patrik Malmström | last post by:
How do I read, write a file binary? I want to open, say, file.exe read it in to the program, then write it out to file2.exe. Like file copy, anyone have a code sample?
4
by: Feng Chun | last post by:
Hi, need help for this. In asp.net, when I do a load balancing on my website, which means there are 2 or more servers behind, one of my webpage needs to read/write a temp file in the server. But...
3
by: erictran | last post by:
Gday everyone, Please help me. How to read and write a pointer of character to a file? Thanks in advance. Eric
5
by: sheriff | last post by:
Dear friends, im a newbee for this forum and c++ im doing my MSc in Simulation Tech in mech. Engineering. My knowledge of c++ is very little which I had during my UG studies Long long ago .I am...
4
by: Bruno | last post by:
Hi! I have big .txt file which i want to read, process and write to another .txt file. I have done script for that, but im having problem with croatian characters (Å ,Ä,Ž,ÄŒ,Ć). How can I...
0
by: Brian Pinto | last post by:
Im facing a problem with file stream read/write for a usb device. The code works fine on XP (32 bit). but fails to work on Windows 7 (64 bit). I'm successfully past problem of getting the device to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.