Reputation: 1
Columns with Null ByteHow can I replace the NULL byte with '0' value after opening up csv file?
My code is as follow but it doesn't work:
try:
# open source file
with open (dataFile,'r')as csvfile:
sourceDF = csv.reader(csvfile)
replaced = [sourceDF.replace(b'\0',b'0') for sourceDF in replaced]
print(replaced)
first_line = True
selHeaders = []
# read each row in source file
for dataRow in sourceDF:
# check if first line of file
if first_line == True:
first_line = False
first_row = dataRow
# check if first file in compile list
if first_run == 0:
result.append(list())
Attached hyper link for the csv files for your reference: https://drive.google.com/drive/folders/1bPbE3hnO7ZAQEVTQ4prHUkEkqlDa7QTi?usp=share_link
Best regards
Tried following code but doesn't work
replaced = [sourceDF.replace(b'\0',b'0') for sourceDF in replaced]
print(replaced)
Upvotes: 0
Views: 80
Reputation: 59440
There are multiple problems in these two lines:
sourceDF = csv.reader(csvfile)
replaced = [sourceDF.replace(b'\0',b'0') for sourceDF in replaced]
The first line reads data from the CSV file. replaced
is None
at this time.
The second line now tries to iterate over replaced
, which is None
- which doesn't work. But even if it would work, it would immediately replace sourceDF
with the things it iterates over, thus making the CSV data disappear.
I'm not sure whether replacing \0
by 0
is a good idea. In my 25 years of coding, I never replaced \0
by a visible character. I only replaced it by nothing, space or \n
. But I can't really judge in your case, because I don't understand what kind of data you have and what the numbers mean.
Upvotes: 2