问题
I have a csv file with some data, I have names in column b and some related information in column A. However not all names have related information
A B
1 JOHN
2 JANE
3 jane@email.com
4 phone:0800etcetc
5 ZAIN
6 MIKE
7 email
8 phon et etc
I am trying to write a code that will read column A if there is information in column A I want it to print the name before that in column B and than print the information with that name.
I hope it makes sense. Please let me know if there is a way of doing that.
This is the output i want. I thank you all for help.
A B
1 John
2 JANE jane@email.com
3 JANE phone:0800 etc etc
4 ZAIN
5 MIKE email
6 MIKE Phone
回答1:
Use the csv library:
So, something like this:
import csv
with open('tmp.csv', 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
name = None
for row in reader:
if row[1]:
name = row[1]
print(name)
else:
print(' '.join([name, row[0]]))
output:
B
John
JANE
JANE jane@email.com
JANE phone:foo
ZAIN
MIKE
MIKE email
MIKE phone
I don't see a way of distinguishing with lookback.
Usually, if you're using Pandas you could use the built in functionality.
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html
import pandas as pd
df = pd.read_csv('your_file.csv')
Note: its bad practice to read/write csv files directly. You should use either the csv library (import csv) or pandas. The reason for this is that they handle special characters, escape types, provide delimiter options and all of the edge cases that happen in a production environment over time.
回答2:
You should try something like:
import sys
csvFile = open('pathOfCSV','r')
for line in csvFile.readlines():
l = line.split()
if len(l) > 1: # informations in columns A and B
sys.stdout.write('ColumnA : %s, ColumnB : %s\n'%(l[0],l[1]))
csvFile.close()
This work if your csv file is not too big, if not you must use readline()function and batch your file.
回答3:
Not sure of what you want to do, but you can read your file with this:
out_file = open("out_file.csv", "w", encoding="your encoding")
with open("file.csv", "r", encoding="your encoding") as file:
for line in file:
cut = line.split("your separator")
colA = cut[0]
colB = cut[1]
# you can output result with:
output = colA + "\t" # any separator you want
output += colB + "\n"
out_file.write(output)
You can do a test on col A inside the for and change the output.
来源:https://stackoverflow.com/questions/45077165/reading-csv-python