擅长:python、mysql、java
<p>可以将列名动态生成为简单计数器(0、1、2等)。</p>
<p><strong>动态生成列名</p>
<pre><code># Input
data_file = "smallsample.txt"
# Delimiter
data_file_delimiter = ','
# The max column count a line in the file could have
largest_column_count = 0
# Loop the data lines
with open(data_file, 'r') as temp_f:
# Read the lines
lines = temp_f.readlines()
for l in lines:
# Count the column count for the current line
column_count = len(l.split(data_file_delimiter)) + 1
# Set the new most column count
largest_column_count = column_count if largest_column_count < column_count else largest_column_count
# Close file
temp_f.close()
# Generate column names (will be 0, 1, 2, ..., largest_column_count - 1)
column_names = [i for i in range(0, largest_column_count)]
# Read csv
df = pandas.read_csv(data_file, header=None, delimiter=data_file_delimiter, names=column_names)
# print(df)
</code></pre>
<p><a href="https://pandas.pydata.org/pandas-docs/stable/missing_data.html" rel="noreferrer">Missing values</a>将被分配给CSV行没有值的列。</p>