[英]Select a random data from a text file and put it in a specific locations in an Excel spreadsheet
I have a question: how can I select one single random piece of data from a file and put it inside of a .csv
file?我有一个问题:我怎样才能 select 从文件中获取一个随机数据并将其放入.csv
文件中? I know you need to set up the path to locate the file in your computer but I would like to know when to use the random()
method or another method to use random since in C#, random is mostly for integers and doubles.我知道您需要设置路径以在计算机中定位文件,但我想知道何时使用random()
方法或其他方法来使用 random,因为在 C# 中,随机主要用于整数和双精度数。
For example: I have this list of names inside of a text file:例如:我在文本文件中有这个名称列表:
Kenneth
Samuel
Samantha
Catherine
Danielle
Jonathan
Ellen
Valentin
Christopher
Edward
Now, I would like to put one of these names inside a .CSV
file, in a specific location.现在,我想将其中一个名称放入.CSV
文件中的特定位置。 Here is what I've tried so far:这是我迄今为止尝试过的:
namespace ConsoleApp
{
class Program
{
public static List<RecordStructure> CSVRecords = new List<RecordStructure>();
static void Main(string[] args)
{
Console.WriteLine("Data Mask Process");
Console.WriteLine("**************************");
CSVRecords.AddRange(ReadCSVFile());
// some things I wrote for a work :)
string contentF_names = File.ReadAllText(@"C:\\path");
string contentM_names = File.ReadAllText(@"C:\\path");
string contentNames = File.ReadAllText(@"C:\\path");
string contentPlaces = File.ReadAllText(@"C\\path");
// For the random part
Random r = new Random();
var line = contentF_names[r.Next(contentF_names.Length)];
// Printing and ending
Console.WriteLine(CSVRecords[1].firstname);
Console.WriteLine(CSVRecords[1].lastname);
Console.WriteLine(CSVRecords[1].city);
Console.WriteLine("**************************");
Console.WriteLine("End of Process.");
Console.ReadKey();
}
public static List<RecordStructure> ReadCSVFile()
{
List<RecordStructure> RecordList = new List<RecordStructure>();
StreamReader reader = new StreamReader(File.OpenRead(@"path"));
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
if (!String.IsNullOrWhiteSpace(line))
{
string[] values = line.Split(',');
RecordList.Add(new RecordStructure
{
firstname = values[0],
lastname = values[1],
company_name = values[2],
city = values[3]
});
}
}
return RecordList;
}
}
class RecordStructure
{
public string firstname { get; set; }
public string lastname { get; set; }
public string company_name { get; set; }
public string address { get; set; }
public string city { get; set; }
public string country { get; set; }
public string state { get; set; }
public string zip { get; set; }
public string phone1 { get; set; }
public string phone2 { get; set; }
public string email { get; set; }
public string web { get; set; }
}
Since you're working on CVS
files, you can convert the result to Array
or List
, then use its index
with the Random
to get a random index.由于您正在处理CVS
文件,因此您可以将结果转换为Array
或List
,然后将其index
与Random
一起使用以获取随机索引。
Example:例子:
var contentF_names = "Kenneth,Samuel,Samantha,Catherine,Danielle,Jonathan,Ellen,Valentin,Christopher,Edward";
// ToList
var contentF_namesArray = contentF_names.Split(',').ToList();
//For the random part
Random r = new Random();
// Get Random index between 0 and the total number of elements in the list
var randomIndex = r.Next(0, contentF_namesArray.Count);
// Use the random index to get an element from the list.
var randomFirstName = contentF_namesArray[randomIndex];
the randomFirstName
will hold the random name, and each time you run. randomFirstName
将保存随机名称,并且每次运行时。 You could use a loop to get x number of random names.您可以使用循环来获取 x 个随机名称。
Example:例子:
/*
Get Five Random Names
*/
for (int x =0; x <= 5; x++)
{
// Get Random index between 0 and the total number of elements in the list
var randomIndex = r.Next(0, contentF_namesArray.Count);
// Use the random index to get an element from the list.
var randomFirstName = contentF_namesArray[randomIndex];
Console.WriteLine(randomFirstName);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.