Hello All:
I am new to Python, and I love it!! I am running 2.6 on Windows. I
have a flat text file here is an example of 3 lines with numbers
changed for security:
999999999088869199999999990200810999999
999999999088869199999999990200810999999
999999999088869199999999990200810999999
I want to be able to replace specific slices with other values. My
code below reads a file into a list of strings. Since strings are
immutable I can't assign different values to a specific slice of the
string. How can I accomplish this? I read some posts on string
formatting but I am having trouble seeing how I can use those features
of the language to solve this problem.
The code below just puts an 'R' at the beginning of each line like
this:
R999999999088869199999999990200810999999
R999999999088869199999999990200810999999
R999999999088869199999999990200810999999
But what I want to do is change the middle of the string. Like this:
R999999999088869CHANGED99990200810999999
R999999999088869CHANGED99990200810999999
R999999999088869CHANGED99990200810999999
#My Current Code
# read the data file in as a list
F = open('C:\\path\\to\file', "r")
List = F.readlines()
F.close()
#Loop through the file and format each line
a=len(List)
while a 0:
List.insert(a,"2")
a=a-1
# write the changed data (list) to a file
FileOut = open("C:\\path\\to\\file", "w")
FileOut.writelines(List)
FileOut.close()
Thanks for any help and thanks for helping us newbies,
-Ben 4 1060
Ben <bm*******@gmail.comwrote:
Since strings are
immutable I can't assign different values to a specific slice of the
string. How can I accomplish this?
....
#My Current Code
# read the data file in as a list
F = open('C:\\path\\to\file', "r")
List = F.readlines()
F.close()
#Loop through the file and format each line
a=len(List)
while a 0:
List.insert(a,"2")
a=a-1
# write the changed data (list) to a file
FileOut = open("C:\\path\\to\\file", "w")
FileOut.writelines(List)
FileOut.close()
Thanks for any help and thanks for helping us newbies,
You probably don't want to use readlines. It is almost always cleaner just
to iterate over the file itself.
You can't change an existing string, but creating a new string isn't
exactly rocket science.
lines = []
for line in F:
modified = line[:16] + "CHANGED" + line[23:]
# or maybe
modified = line.replace("1999999", "CHANGED")
lines.append(modified)
FileOut.writelines(lines)
To modify a file inplace have a look at the fileinput module.
On Tue, 2008-10-21 at 10:28 -0700, Ben wrote:
Hello All:
I am new to Python, and I love it!! I am running 2.6 on Windows. I
have a flat text file here is an example of 3 lines with numbers
changed for security:
999999999088869199999999990200810999999
999999999088869199999999990200810999999
999999999088869199999999990200810999999
I want to be able to replace specific slices with other values. My
code below reads a file into a list of strings. Since strings are
immutable I can't assign different values to a specific slice of the
string. How can I accomplish this? I read some posts on string
formatting but I am having trouble seeing how I can use those features
of the language to solve this problem.
The code below just puts an 'R' at the beginning of each line like
this:
R999999999088869199999999990200810999999
R999999999088869199999999990200810999999
R999999999088869199999999990200810999999
But what I want to do is change the middle of the string. Like this:
R999999999088869CHANGED99990200810999999
R999999999088869CHANGED99990200810999999
R999999999088869CHANGED99990200810999999
Well, it depends on what you want. Do you want to replace by location
or by matched substring? One of the following functions might help.
lines = ['999999999088869199999999990200810999999'
'99999999088869199999999990200810999999'
'9999999088869199999999990200810999999']
def replace_by_location(string, replacement, start, end):
return string[:start] + replacement + string[end:]
def replace_by_match(string, substr, replacement):
return replacement.join(string.split(substr))
location_lines = [replace_by_location(x, 'CHANGED', 15, 22) for x in lines]
match_lines = [replace_by_match(x, '1999999', 'CHANGED') for x in lines]
print location_lines
print match_lines
Cheers,
Cliff
#My Current Code
# read the data file in as a list
F = open('C:\\path\\to\file', "r")
List = F.readlines()
F.close()
#Loop through the file and format each line
a=len(List)
while a 0:
List.insert(a,"2")
a=a-1
# write the changed data (list) to a file
FileOut = open("C:\\path\\to\\file", "w")
FileOut.writelines(List)
FileOut.close()
Thanks for any help and thanks for helping us newbies,
-Ben
-- http://mail.python.org/mailman/listinfo/python-list
On Oct 21, 1:53*pm, "J. Cliff Dyer" <j...@sdf.lonestar.orgwrote:
On Tue, 2008-10-21 at 10:28 -0700, Ben wrote:
Hello All:
I am new to Python, and I love it!! I am running 2.6 on Windows. I
have a flat text file here is an example of 3 lines with numbers
changed for security:
999999999088869199999999990200810999999
999999999088869199999999990200810999999
999999999088869199999999990200810999999
I want to be able to replace specific slices with other values. My
code below reads a file into a list of strings. Since strings are
immutable I can't assign different values to a specific slice of the
string. How can I accomplish this? I read some posts on string
formatting but I am having trouble seeing how I can use those features
of the language to solve this problem.
The code below just puts an 'R' at the beginning of each line like
this:
R999999999088869199999999990200810999999
R999999999088869199999999990200810999999
R999999999088869199999999990200810999999
But what I want to do is change the middle of the string. Like this:
R999999999088869CHANGED99990200810999999
R999999999088869CHANGED99990200810999999
R999999999088869CHANGED99990200810999999
Well, it depends on what you want. *Do you want to replace by location
or by matched substring? *One of the following functions might help.
lines = ['999999999088869199999999990200810999999'
* * * * *'99999999088869199999999990200810999999'
* * * * *'9999999088869199999999990200810999999']
def replace_by_location(string, replacement, start, end):
* * return string[:start] + replacement + string[end:]
def replace_by_match(string, substr, replacement):
* * return replacement.join(string.split(substr))
location_lines = [replace_by_location(x, 'CHANGED', 15, 22) for x in lines]
match_lines = [replace_by_match(x, '1999999', 'CHANGED') for x in lines]
print location_lines
print match_lines
Cheers,
Cliff
#My Current Code
# read the data file in as a list
F = open('C:\\path\\to\file', "r")
List = F.readlines()
F.close()
#Loop through the file and format each line
a=len(List)
while a 0:
* * List.insert(a,"2")
* * a=a-1
# write the changed data (list) to a file
FileOut = open("C:\\path\\to\\file", "w")
FileOut.writelines(List)
FileOut.close()
Thanks for any help and thanks for helping us newbies,
-Ben
-- http://mail.python.org/mailman/listinfo/python-list
Thanks for your help, this explains it I needed a little mental jump
start.
-Ben
Duncan Booth wrote:
Ben <bm*******@gmail.comwrote:
>Since strings are immutable I can't assign different values to a specific slice of the string. How can I accomplish this?
You probably don't want to use readlines. It is almost always cleaner just
to iterate over the file itself.
You can't change an existing string, but creating a new string isn't
exactly rocket science.
lines = []
for line in F:
modified = line[:16] + "CHANGED" + line[23:]
# or maybe
modified = line.replace("1999999", "CHANGED")
lines.append(modified)
FileOut.writelines(lines)
If you want your program to scale to indefinitely large files, and you
edit each line independently, without using date from other lines, read
a line *and* write the new line immediately
for line in F:
FileOut.write(<modified>)
Where <modifiedis one of the suggested expressions, or a temporary
variable calculated on multiple lines.
To edit a line 'in-place', one can use the array module for mutable
array of bytes (or the 3.0 bytearray), but I would only both with
extensive editing. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Noen |
last post by:
def XOR(s1,s2):
""" XOR string s1 with s2 """
output = ""
# Argument check
if (type(s1) and type(s2)) != type(""):
raise TypeError, "Arguments are not strings"
if len(s1) != len(s2):
raise...
|
by: Poonam |
last post by:
Hi
Can we align an image or content vertically middle in a DIV.
tks poonam
|
by: Ian Williamson |
last post by:
Greetings,
My company has an ASP.NET based enterprise product that is undergoing some
changes and I need some community input to help solve a problem.
In the current implementation, any given...
|
by: Kza |
last post by:
Hi, just in the process of maintaining some software that used some
funy old string library and char*s , and we are updating everything to
use std::strings. (or should I say std::basic_string<>s)
...
|
by: Matt Colegrove |
last post by:
I'm working on a web app that is published to a hosting service. I'm
developing it on my local PC with VS 2005 and SQL Express. The hosting
service DB is SQL Server 2000.
I have two...
|
by: Bob |
last post by:
Over the life of a distributed app, it is possible for the connection string
that it was configured with initailly needs to change.
You can't scope a connection string setting to user so that it...
|
by: Matthew Warren |
last post by:
Hi,
I would expect this to work,
rawstring=r'some things\new things\some other things\'
But it fails as the last backslash escapes the single quote.
...although writing this I think I...
|
by: xkeops |
last post by:
I have forgotten how vb.net deals with long strings, I know I used
this once but I don't remember.
Or was it C# ???
dim s as string = @"string1
string2
string3"
instead of
|
by: hstagni |
last post by:
Where can I find a library to created text-based windows applications?
Im looking for a library that can make windows and buttons inside
console.. Many old apps were make like this, i guess
...
|
by: Academia |
last post by:
As a simple example suppose I want to draw two columns of strings.
I know where each column starts and the widths.
So before I print a string I measure it and if it is longer than the column...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |