0

I am getting the following error "Error: line contains NULL byte" when trying to read and covert a CSV column into a dictionary list within Python.

I have followed list contains NULL byte, CSV DictReader but this doesn't seem to work with the code I have written.

import pandas as pd
import csv
from collections import defaultdict

columns = defaultdict(list) 

with open('file.csv') as f:
    reader = csv.DictReader(f) 
    for row in reader: 
        for (k,v) in row.items(): 
            columns[k].append(v) 
                                 

keywords = print(columns['Keyword'])

Any help would be appreciated! Thanks.

1 Answer 1

0

The problem as you see is the empty row/cell. The link you mentioned is replacing '\0' with a dummy string ''

Do you have any other content in the 'file.csv' which is causing this error? Can you check its content first? I believe the error you get would need replacing some other problematic string with dummy string ''

An easier way is to just print all the unique characters and check whether you have problematic strings like '\0', '\0\0', '\0,\0' etc

If you are only looking to convert a column to the list you can try this:

df = pd.read_csv("file.csv")
df["Keyord"].tolist()
Sign up to request clarification or add additional context in comments.

4 Comments

Yeah the CSV i am reading has multiple rows and columns. The script I have, I just want the content of the "Keyword" column, and then convert that into a list.
If that is the case, can you try df = pd.read_csv("file.csv") and df['Keyword"].tolist()
Thanks so far, now getting a "UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte" error when reading the file.
It seems that encoding is different for the file you have. See this stackoverflow.com/questions/33819557/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.