简体   繁体   中英

How to keep the multiple lines in CSV, in Python

This is a final dataframe from a CSV file I read in Python: CSV输入 (except I am losing the multiple lines, descriptions are all one line)

I want to keep the multiple lines in the description as they are but Python condensed them into one line. I used pandas and numpy libraries so far. I have the option to read from CSV and xls, I am currently reading from CSV. Then I want to turn this into the following table:

Questions:

  • how can I move the names on top of the description
  • how can I move the seniority next to the name in large red font
  • how can I group by the level as you see it looks like a pivot table
  • where it says << some math calculation here >> is where I will take two values from other columns and subtract them. Ex: val1-val2

This is the desired output where the multiple lines are preserved:

这是保留多行的期望​​输出 Wtdfs.png

How can I move the names on top of the description?

This is simply string concatenation in Python, eg:

row[2] = row[1] + "\n" + row[2]

How can I move the seniority next to the name in large red font

CSV doesn't provide the ability to do formatting like in a spreadsheet format as .xls or similar. If you export a spreadsheet in CSV format you'll see that it's formatting like bold or color is discarded.

How can I group by the level as you see it looks like a pivot table?

You can create a dictionary like this:

pivot = {}
for row in rows:
  if row[3] in pivot:
    pivot[row[3]] = [row]
  else:
    pivot[row[3]].append(row)

where it says is where I will take two values from other columns and subtract them. Ex: val1-val2

You can do that calculation then replace the string, eg:

row[2] = row[2].replace("<< some math calculation here >> ", x)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM